(android 地图实战开发)2 创建MapActivity,根据设备当前位置,显示地图
2011-09-13 14:12
661 查看
实现效果:
获取手机gps当前的位置,显示位置对于的google地图.
具体的步骤:
1 Step One 创建包含MapView控件的应用界面
<com.google.android.maps.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);
下面是完整的实现代码:
界面代码:
后台代码:
获取手机gps当前的位置,显示位置对于的google地图.
具体的步骤:
1 Step One 创建包含MapView控件的应用界面
<com.google.android.maps.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);
下面是完整的实现代码:
界面代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.google.android.maps.MapView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/map_view" android:clickable="true" android:apiKey="0zoMLU7jtw20HSV-bpo6p81D6rFEvGwMz2BJqiw" /> </LinearLayout>
后台代码:
publicclass LocationMap extends MapActivity { LocationManager locationManager; @Override publicvoid onCreate(Bundle savedInstanceState) { 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 publicboolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK){ ViewUtility.NavigateActivate(LocationMap.this, Main.class); } returnfalse; } @Override protectedboolean isRouteDisplayed() { // TODO Auto-generated method stub returnfalse; } 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 publicvoid onLocationChanged(Location location) { Log.d("Location", "onLocationChanged"); } //provider失效时调用 @Override publicvoid onProviderDisabled(String provider) { Log.d("Location", "onProviderDisabled"); } //provider启用时调用 @Override publicvoid onProviderEnabled(String provider) { Log.d("Location", "onProviderEnabled"); } //状态改变时调用 @Override publicvoid onStatusChanged(String provider, int status, Bundle extras) { Log.d("Location", "onStatusChanged"); } }; }
相关文章推荐
- (android 地图实战开发)2 创建MapActivity,根据设备当前位置,显示地图
- (android 地图实战开发)2 创建MapActivity,根据设备当前位置,显示地图
- (android 地图实战开发)3 在地图上显示当前位置和自定义银行位置
- Android百度地图开发之显示当前位置地图
- 百度地图开发,在地图上显示当前位置
- Android移动开发-通过自定义算法代码来纠偏地图由GPS定位到的经纬度在地图上显示或解析位置时出现偏移的问题
- Android开发仿QQ空间根据位置弹出PopupWindow显示更多操作效果
- 学习iOS开发之设置地图初始显示区域为当前位置
- android百度地图开发V4.5最新版(4)---显示自己的位置在地图上
- (android地图开发) 基于百度Lbs服务定位用户的当前位置
- Android高德地图获取当前地理位置(不显示地图只获取当前位置)
- Android基于mAppWidget实现手绘地图(十二)–显示当前用户位置
- Android 高德地图,显示地图,并定位当前的位置
- android,使用高德地图SDk(地图SDK,定位SDK,搜索SDK),显示当前位置以及自定义多个Markers
- Android初级开发(十)——百度定位—在地图上显示你的位置
- iOS 开发 地图显示用户当前位置
- [原创]Android自定义View之IndicatorView,显示当前tab页所处位置的View
- Android Map Api 使用和开发(2) 定位我的位置、地图弹出泡泡、通过经纬度获取地址
- iOS 地图显示比例设置 (这里是当前的位置信息代理方法中实现)
- Android使用百度地图SDK获得当前设备位置所在的省、市