当前位置: 代码迷 >> Android >> android 下google 地图的使用
  详细解决方案

android 下google 地图的使用

热度:78   发布时间:2016-05-01 14:35:20.0
android 上google map的使用

?

一.MAP API密钥的申请
Eclipse->Window->Preferences->Android->Build中查看debugkeystore的位置。
cmd中执行
keytool -list -alias androiddebugkey -keystore “ …..” -storepass android -keypass android??? 其中””中的是你自己刚得到的keystore的位置。若要求输密码则是“android” 得到认证指纹B9:BD:E8:7B:B2:21:B7:9E:15:12:70:44:12:30:62:B0
到打开http://code.google.com/intl/zh-CN/android/maps-api-signup.html填入刚 申请到的认证指纹(MD5)就可以获得apikey
Apikey的使用:
?layout中加入MapView
                     <com.google.android.maps.MapView         android:id="@+id/mapview"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:apiKey=" 0gn_orY7fE7XNXBtOjG7GSsNcPkhoszWbvVs2CQ " /> 
模拟器的设置:??建立一个"GoogleInc.:Google APIs:3"的模拟器
二.项目的建立:
建立新项目时BuildTarget 应该选择Google APIs,将maps.jar引入。在\add-ones
三.权限和Maps库设置
manifest.xml中设置全相应的权限,比如: ?
                      <uses-permission android:name="android.permission.access_COARSE_LOCATION"/>        <uses-permission android:name="android.permission.INTERNET" />
?manifest.xml中加上要用的maps库:
                      <uses-library android:name="com.google.android.maps" />
?四.Maps库分析
1MapController
控制地图移动,伸缩,以某个GPS坐标为中心,控制MapView中的view组件,管理Overlay,提供View的基本功能。
常用方法:animateTo(GeoPoint point)? setCenter(GeoPointpoint)? setZoom(int zoomLevel) 等。
2MapView
Mapview是用来显示地图的view, 它派生自android.view.ViewGroup。当MapView获得焦点,可以控制地图的移动和缩放。
地图可以以不同的形式来显示出来,如街景模式,卫星模式等,通过setSatellite(boolean)? setTraffic(boolean),setStreetView(boolean) 方法。
四.Maps库分析

1)MapController

?????????? 控制地图移动,伸缩,以某个GPS坐标为中心,控制MapView中的view组件,管理Overlay,提供View的基本功能。

??????常用方法:animateTo(GeoPoint point)? setCenter(GeoPointpoint)? setZoom(int zoomLevel) 等。

?

2)MapView

Mapview是用来显示地图的view, 它派生自android.view.ViewGroup。当MapView获得焦点,可以控制地图的移动和缩放。

地图可以以不同的形式来显示出来,如街景模式,卫星模式等,通过setSatellite(boolean)? setTraffic(boolean), setStreetView(boolean) 方法。

3MapActivity

??????管理Activity的生命周期,为mapview建立及取消对mapservice的连接。

??????MapActivity是一个抽象类,任何想要显示MapViewactivity都需要派生自MapActivity。并且在其派生类的onCreate()中,都要创建一个MapView实例,可以通过layout XML来创建。

4Overlay

?????Overlay是覆盖到MapView的最上层,可以扩展其ondraw接口,自定义在MapView中显示一些自己的东西。MapView通过MapView.getOverlays()Overlay进行管理。

5)MylocationOverlay

??????集成了Android.location中接收当前坐标的接口,集成SersorManagerCCompassSensor的接口,我们只需要enableMyLocation(),enableCompass就可以让我们的程序拥有实时的MyLocation以及Compass 功能。

6ItemlizedOverlay

???它包含了一个OverlayItems列表。它为每个点绘制标记点,和维护一个焦点选中的item,同时也负责把一个屏幕点击匹配到item上去,分发焦点改变事件给备选的监听器。

7ProjectionMapViewGPS坐标与设备坐标的转换(GeoPointPoint)。

五.移动和缩放的实现:

1)移动:

                        GeoPoint pt = new  GeoPoint(mapView.getMapCenter().getLatitudeE6(),           mapView.getMapCenter().getLongitudeE6()+ mapView.getLongitudeSpan() / 4);           mc.setCenter(pt);
?

?

2)缩放:

            mapView.setBuiltInZoomControls(true);   public void zoomIn() {           mc.zoomIn();       }       public void zoomOut() {           mc.zoomOut();      }
?
六.地图模式的选择:

?

? 地图模式主要有街景模式,交通模式,卫星模式。

?

????? 通过mapView.setTraffic()?? mapView.setSatellite()?? mapView.setStreetView()?来设置使用街景模式。

???例如:

   public void setStreetView() {        mapView.setTraffic(false);        mapView.setSatellite(false);        mapView.setStreetView(true);  }

?

???? 调用方式:? mapView.setStreetView();

?

六.GPS定位

1MyLocationOverlay:? maps.jar map上增加一个显示定位地址的overlay

? enableMyLocation()

? runOnFirstFix()

2LocationManager:? (location.jar)?

? getBastProvider():得到最佳的位置提供者。

? getLastKnownLocation():得到当前的位置。

??

3MyLocationOverlay:? maps.jar map上增加一个显示定位地址的overlay

? enableMyLocation():尝试开启MyLocation功能,

? runOnFirstFix():把一个runnable加入队列,一旦收到一个位置信息,这个runnable就被执行。

4LocationManager:? (location.jar)?

? getBastProvider():得到最佳的位置提供者。

? getLastKnownLocation():得到当前的位置。

5LocationListener接口(location) 我们可以实现onLocationChanged(Location loc)方法来动态的监听位置的变化。

?

private void initMyLocation() {loverlay = new MyLocationOverlay(this, mapView);loverlay.enableMyLocation();loverlay.runOnFirstFix(new Runnable() {public void run() {mc = mapView.getController();mc.animateTo(loverlay.getMyLocation());}});mapView.getOverlays().add(loverlay);}LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);			String provider = locationManager.getBestProvider(criteria, true);	Location location = locationManager.getLastKnownLocation(provider);		Double latitude = location.getLatitude() * 1E6;		Double longitude = location.getLongitude() * 1E6;		geoPoint = new GeoPoint(latitude.intValue(), longitude.intValue());

?七.添加图钉

1)通过继承mapsoverlay后重写draw()方法。适合添加某一个图钉。

2)通过继承mapsItemizedOverlay后重写draw()方法,适合添加一批相同的图钉,并维护每个图钉的焦点。

            List<OverlayItem> locations = new ArrayList<OverlayItem>();   populate();public void draw(Canvas canvas, MapView mapView, boolean shadow) {super.draw(canvas, mapView, shadow);boundCenterBottom(marker);}


?

?

七.地图搜索

?

1Geocoder

? ?getFromLocation(double latitude, double longitude, int maxResults)

? ?getFromLocationName(String locationName, int maxResults, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude)

? ?getFromLocationName(String locationName, int maxResults)

?

?

?

  相关解决方案