android 上google map的使用

 

一. MAP API 密钥的申请 :
① 在 Eclipse->Window->Preferences->Android->Build 中查看 debug keystore 的位置。
② 在 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 " /> 
模拟器的设置:    建立一个 "Google Inc.:Google APIs:3" 的模拟器
二. 项目的建立:
建立新项目时 Build Target 应该选择 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 库分析
1 ) MapController
控制地图移动,伸缩,以某个 GPS 坐标为中心,控制 MapView 中的 view 组件,管理 Overlay ,提供 View 的基本功能。
常用方法: animateTo ( GeoPoint point)  setCenter ( GeoPoint point)  setZoom ( int zoomLevel ) 等。
2 ) MapView
Mapview 是用来显示地图的 view, 它派生自 android.view.ViewGroup 。当 MapView 获得焦点,可以控制地图的移动和缩放。
地图可以以不同的形式来显示出来,如街景模式,卫星模式等,通过 setSatellite ( boolean )  setTraffic ( boolean ), setStreetView ( boolean ) 方法。
四.Maps库分析

1)MapController

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

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

 

2)MapView

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

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

3)MapActivity

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

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

4)Overlay

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

5) MylocationOverlay

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

6)ItemlizedOverlay

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

7)Projection:MapView中GPS坐标与设备坐标的转换(GeoPoint和Point)。

五.移动和缩放的实现:

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 定位

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

  enableMyLocation()

  runOnFirstFix()

2)LocationManager :  (location.jar) 

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

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

  

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

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

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

4)LocationManager :  (location.jar) 

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

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

5)LocationListener接口 (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)通过继承maps的overlay后重写draw()方法。适合添加某一个图钉。

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

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


 

 

七. 地图搜索

 

1)Geocoder

   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)

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值