你有两种方式确定用户的当前位置:
1.使用安卓标准方式
2. 使用MapWidget的监听器
标准方式:
在你的acivity中加入:
LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME,MIN_DISTANCE, new LocationListener() { public void onStatusChanged(String provider, int status, Bundle extras) { } public void onProviderEnabled(String provider) { } public void onProviderDisabled(String provider) { } public void onLocationChanged(Location location) { // Handle location change here }});
使用MapWidget的监听器
MapWidget使用Network location provider ,以便快速定位,或者使用GPS location provider
下面是如何使用监听器的示例:
private void initListeners(){ mapWidget.setOnLocationChangedListener(new OnLocationChangedListener() { public void onLocationChanged(MapWidget v, Location location) { Layer layer = v.getLayerById(LAYER_ID); MapObject object = layer.getMapObject(OBJECT_ID); object.moveTo(location); } });}
当然,需要配置请求定位的时间间隔,使用以下代码片段:
private void configureMapWidget(){ GPSConfig gpsConfig = mapWidget.getGpsConfig(); gpsConfig.setGPSUpdateInterval(MIN_TIME_INTERVAL, MIN_DISTANCE_IN_METERS);}