当前位置:嗨网首页>书籍在线阅读

04-地图定位

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

20.2.1 地图定位

地图定位功能能实现对电动车位置的定位,用户在启动导航系统后将自动定位当前的地理位置,并且可以通过放大缩小来查看周边的环境。该功能通过DingwActivity.java实现。关键源代码如下。

public class DingwActivity extends MapActivity //从MapActivity类继承 { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.wherelayout); //设置显示布局 linearLayout=(LinearLayout)findViewById(R.id.zoomview); mapView=(MapView)findViewById(R.id.mapview); mZoom=(ZoomControls)mapView.getZoomControls(); linearLayout.addView(mZoom); //添加视图到布局中 /为当前所在点添加图层标示/ /从MapView中获得MapController对象,调用locate()方法完成定位/ MapController controller=mapView.getController(); GeoPoint point=locate(controller); mapOverlays=mapView.getOverlays(); /获得该图标对象Drawable/ drawable=this.getResources().getDrawable(R.drawable.mappin_red); itemizedOverlay=new MyPositionItemizedOverlay(drawable); OverlayItem overlayitem=new OverlayItem(point,"",""); itemizedOverlay.addOverlay(overlayitem); mapOverlays.add(itemizedOverlay); } /获得当前经纬度信息,通过MapController定位到该点/ private GeoPoint locate(MapController controller){ locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE); String provider=LocationManager.GPS_PROVIDER; Location location=locationManager.getLastKnownLocation(provider); double lat=0.0; double lng=0.0; if(location!=null){ lat=location.getLatitude(); lng=location.getLongitude(); }else{ lat=24.970463; lng=121.266947; } GeoPoint point=new GeoPoint((int)(lat1E6),(int)(lng1E6)); controller.animateTo(point); return point; }