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

来源:互联网 发布:js 数组 排序 编辑:程序博客网 时间:2024/06/05 03:11

http://www.cnblogs.com/macroxu-1982/archive/2011/09/13/2174657.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 stubreturnfalse;    }        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");             }     };            }

0 0
原创粉丝点击