(android 地图实战开发)2 创建MapActivity,根据设备当前位置,显示地图
2015-12-07 15:57
603 查看
(android 地图实战开发)2 创建MapActivity,根据设备当前位置,显示地图
/article/4749541.html实现效果:
获取手机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 Graphics.drawable之—ShapeDrawable学习
- Android调用JNI的进阶实例(摄像头预览数据转码RGB播放)
- android 5.0新特性,recyclerview的基本用法,及应注意的问题
- H5与Android之间的交互
- android(36)(开启服务与终止服务)
- Android通过剪切板传递数据
- Android——蓝牙通信
- 自定义Android progress bars
- 老猪带你玩转android自定义控件二——自定义索引栏listview
- Android 调用系统相机拍照的返回结果
- Android测试教程13--模拟服务器MockServer之Moco详细介绍
- android 事件总线 -- Otto(六) Bus
- 如何正确获得Android内外SD卡路径
- (Android)五分钟学会PopupWindow
- 无法启动Android模拟器的解决办法
- Android自定义Spinner
- android 百度地图 驾车路径的距离获取
- 利用HTML5开发Android
- android动画
- android使用html+javascript来制作页面