(android 地图实战开发)2 创建MapActivity,根据设备当前位置,显示地图


实现效果:
获取手机gps当前的位置,显示位置对于的google地图.

(android 地图实战开发)2 创建MapActivity,根据设备当前位置,显示地图
具体的步骤:
1 Step _disibledevent=>创建包含MapView控件的应用界面
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/map_view"
android:clickable="true"
android:apiKey="0zoMLU7jtw20HSV-bpo6p81D6rFEvGwMz2BJqiw"
/>
在这里主要有两个属性 clickable 是否允许点击,true ,将捕获点击事件,并作出基于位置的响应.
apiKey :注册并允许基于地图开发的key,具体如何获得apikey 看上一篇
开发环境下配置google地图功能步骤

2 Step Two 初始化MapView
MapView map_view =(MapView)findViewById(R.id.map_view);
map_view.setStreetView(true);
map_view.setTraffic(true);
map_view.setBuiltInZoomControls(true);
map_view.setSatellite(false);

主要几个方法说明:
setStreetView (true) 是否显示街道信息
setTraffic(true) 是否显示交通信息
setSatellite(false)是否显示卫星图,如True显示卫星图,false 显示地图

3 Step Three 获取终端GPS位置
l 获取服务位置服务
String context=Context.LOCATION_SERVICE;
locationManager=(LocationManager)getSystemService(context);
String provider=LocationManager.GPS_PROVIDER;

l 获取当前地理位置
1 获取定位服务的最后一次定位信息
location =locationManager.getLastKnownLocation(provider);
2 更新当前的定位信息
locationManager.requestLocationUpdates(provider, 0, 0, locationListener );

4 Step Four 显示定位下的地图信息
MapController mapcontroller=map_view.getController();

GeoPoint point=getCurrentPoint();// 设置地图的当前位置信息
mapcontroller.setCenter(point);
mapcontroller.setZoom(20);
mapcontroller.animateTo(point);

下面是完整的实现代码:
界面代码:

后台代码:
public class LocationMap extends MapActivity { LocationManager locationManager; @Override public void _disibledevent=>super.onCreate(savedInstanceState); setContentView(R.layout.locationmap); //设置地图control MapView map_view =(MapView)findViewById(R.id.map_view); map_view.setStreetView(true); map_view.setTraffic(true); map_view.setBuiltInZoomControls(true); map_view.setSatellite(false); MapController mapcontroller=map_view.getController(); GeoPoint point=getCurrentPoint();// 设置地图的当前位置信息 mapcontroller.setCenter(point); mapcontroller.setZoom(20); mapcontroller.animateTo(point); } @Override public boolean _disibledevent=>int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK){ ViewUtility.NavigateActivate(LocationMap.this, Main.class); } return false; } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } private GeoPoint getCurrentPoint() { String context=Context.LOCATION_SERVICE; locationManager=(LocationManager)getSystemService(context); String provider=LocationManager.GPS_PROVIDER; Location location =locationManager.getLastKnownLocation(provider); if(location == null){ //没有最后位置,更新gps,获取当前位置 locationManager.requestLocationUpdates(provider, 0, 0, locationListener ); location =locationManager.getLastKnownLocation(provider); } GeoPoint point=null; if(location==null) { Double lat=37.422006*1E6; //默认值 Double lng=-122.084095*1E6; point=new GeoPoint(lat.intValue(),lng.intValue()); } else //当前反馈的GPS位置 { Double lat=location.getLatitude()*1E6; Double lng=location.getLongitude()*1E6; point=new GeoPoint(lat.intValue(),lng.intValue()); } return point; } //创建位置监听器 private LocationListener locationListener = new LocationListener(){ //位置发生改变时调用 @Override public void _disibledevent=>"Location", "onLocationChanged"); } //provider失效时调用 @Override public void _disibledevent=>"Location", "onProviderDisabled"); } //provider启用时调用 @Override public void _disibledevent=>"Location", "onProviderEnabled"); } //状态改变时调用 @Override public void _disibledevent=>int status, Bundle extras) { Log.d("Location", "onStatusChanged"); } }; }

特别感谢:卓易点评网 赞助支持

Tags: 

延伸阅读

最新评论

发表评论