擴增實境

擴增實境

將地點或服務機構位置,顯示在擴增實境功能,使用者能在相機螢幕上進行顯示與互動擴增實境程式範例如下,使用者於手持裝置中開啟擴增實境功能,設定標註點坐標及名稱等進行顯示,範例是以台北市內湖區西湖捷運站進行擴增實境功能展示,設定距離標記點位距離1000公尺時方才顯示:

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

try

{

mapView = new TGOnlineMap(this);  //new TGOSMapAR會使用到TGOSMap裡的資源

Camera camera = Camera.open();  //開啟相機

supergis.runtime.sdk.utility.Design.FullScreen(this);  //設定全螢幕

setContentView(R.layout.activity_main);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

//鎖定螢幕方向,維持螢幕為PORTRAIT

 

_LocationAdapter = new TGLocationAdapter(this);  //建立GPS相關物件

_SensorAdapter = new TGSensorAdapter(this);  //建立Sensor相關物件      

_SensorAdapter.AddSensorAdapterListener(this);  //加入Listener

_CameraView = new TGCameraView(this,camera);  //建立相機畫面  

 

RelativeLayout  layout =  (RelativeLayout )(findViewById(R.id.CameraLayout));  //放置ARLayout

 

//建立AR畫面,需要加入相機、GPSSensor相關資訊

_CameraOverlay = new TGAugmentedReality(this, _CameraView, _SensorAdapter, _LocationAdapter);

_CameraOverlay.setMaxDistance(1000);  //設定距離(公尺)

_CameraOverlay.setOnTouchListener(this);

 

//加入Mark

TGLatLng PT = new TGLatLng(25.082023,121.567025);  //坐標須用WGS84

TGMarkerOptions a = new TGMarkerOptions()

.position(PT)

.title("西湖捷運站");

_CameraOverlay.addMarker(a);

 

//依序把相機畫面及AR畫面加入

layout.addView(_CameraView);

layout.addView(_CameraOverlay);

 

}

catch(Exception ex)

{

Design.ShowAlert("onCreate", ex.toString());

}

}

 

@Override

protected void onStart()

{

super.onStart();//開啟SensorGPS

_LocationAdapter.Start();

_SensorAdapter.Start();

}

 

@Override

protected void onStop()

{

super.onStop();//關閉SensorGPS

_LocationAdapter.Stop();

_SensorAdapter.Stop();

_CameraView.surfaceDestroyed(null);

}

 

@Override

public void SensorChanged(ISensorAdapter arg0) {

try

{  //手機 Sensor有動作時更新畫面

if(_CameraView != null && _CameraOverlay != null &&

_CameraView.getWidth() <= 0 || _CameraView.getHeight() <= 0)

return;

_CameraOverlay.RefreshOverlay();

}

catch(Exception ex){}

}

 

@Override

public boolean onTouch(View v, MotionEvent event) {

int TX = (int) event.getRawX();

int TY = (int) event.getRawY();

TGMarkerOptions TMarker = _CameraOverlay.HitTest(TX, TY);

if(TMarker != null)

{

Toast.makeText(this, "TMarker:"+TMarker.getTitle(), Toast.LENGTH_LONG).show(); 

}

return false;

}

回到上方