android:百度地圖-給地圖添加標註物

在上篇文章中給你們簡單的搭建了百度地圖開發的基本環境,今天給你們來介紹介紹如何在地圖上面添加標註物java

如對這篇文章有看不懂的地方,請轉戰到上一篇文章-->飛機直達android

在正式開始以前先請你們注意在轉載博客的時候注意說明出處 
api

今天給你們帶來四個方面的結束,ide

第一個:就是介紹地圖顯示交通訊息咱們只須要添加一個代碼就能夠完成工具

 

  1. mapView.setTraffic(true);  


第二個:給地圖顯示衛星地圖,一樣也很簡單,一樣只須要一句代碼學習

 

  1. mapView.setSatellite(true);  


第三個:給地圖設置一個標註物ui

這裏用到的是百度地圖提供的 Overlay 對象  --> 移步百度地圖官方APIthis

首先寫一個內部類繼承自 Overlay 對象spa

 

  1. //標註一個遮蓋物  
  2.     public class MyOverlay extends Overlay{  
  3.         //聲明一個地點  
  4.         private GeoPoint geoPoint = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));  
  5.         //聲明一個畫筆工具  
  6.         private Paint paint = new Paint();  
  7.           
  8.         @Override  
  9.         public void draw(Canvas arg0, MapView arg1, boolean arg2) {  
  10.             super.draw(arg0, arg1, arg2);  
  11.               
  12.             Point point = mapView.getProjection().toPixels(geoPoint, null);  
  13.             arg0.drawText("這裏是天安門", point.x,point.y,paint);  
  14.         }  
  15.     }  

而後在onCreate方法中對該類進行調用.net

 

  1. //v2.00  
  2. //給地圖對象設置標註物  
  3. //mapView.getOverlays().add(new MyOverlay());  
  4. //mapController.setZoom(12);  


這樣就能夠很簡單的使用標註物啦。


第四個:是如何給百度地圖設置多個標註物一樣的道理這裏咱們用到的是百度地圖API裏面提供的 ItemizedOverlay 對象

一樣寫一個內部類繼承自 ItemizedOverlay 對象

  1.   //標註多個遮蓋物  
  2.   public class MyOverLayItem extends ItemizedOverlay<OverlayItem>{  
  3.       
  4.     private List<OverlayItem> overlayItem = new ArrayList<OverlayItem>();  
  5.       
  6.     //定義一組座標  
  7.     private double mLat1 = 39.90923;//經  
  8.     private double mLot1 = 116.397428;//緯  
  9.       
  10.     private double mLat2 = 39.92923;//經  
  11.     private double mLot2 = 116.377428;//緯  
  12.       
  13.     private double mLat3 = 39.94923;//經  
  14.     private double mLot3 = 116.357428;//緯  
  15.       
  16.     private double mLat4 = 39.96923;//經  
  17.     private double mLot4 = 116.337428;//緯  
  18.       
  19.     //用於在地圖上標識座標,用一個圖片標註  
  20. public MyOverLayItem(Drawable drawable) {  
  21.     super(drawable);  
  22.     GeoPoint geoPoint1 = new GeoPoint((int)(mLat1*1E6),(int)(mLot1*1E6));  
  23.     GeoPoint geoPoint2 = new GeoPoint((int)(mLat2*1E6),(int)(mLot2*1E6));  
  24.     GeoPoint geoPoint3 = new GeoPoint((int)(mLat3*1E6),(int)(mLot3*1E6));  
  25.     GeoPoint geoPoint4 = new GeoPoint((int)(mLat4*1E6),(int)(mLot4*1E6));  
  26.       
  27.     overlayItem.add(new OverlayItem(geoPoint1, "A""這是第一個"));  
  28.     overlayItem.add(new OverlayItem(geoPoint2, "B""這是第二個"));  
  29.     overlayItem.add(new OverlayItem(geoPoint3, "C""這是第三個"));  
  30.     overlayItem.add(new OverlayItem(geoPoint4, "D""這是第四個"));  
  31.     //刷新地圖  
  32.     populate();  
  33. }  
  34.   
  35. //返回指定的List集合中每個座標  
  36. @Override  
  37. protected OverlayItem createItem(int arg0) {  
  38.     return overlayItem.get(arg0);  
  39. }  
  40.   
  41. @Override  
  42. public int size() {  
  43.     return overlayItem.size();  
  44. }  
  45.       
  46. //當標註物被點擊的時候  
  47. @Override  
  48. public boolean onTap(int i) {  
  49.     Toast.makeText(BaiDu_SuYiActivity.this, overlayItem.get(i).getSnippet(), 2).show();  
  50.     return true;  
  51. }  
  52.   }  

而後在onCreate方法中對該類進行調用

 

  1. //v3.00  
  2. //給地圖設置多個標註物  
  3. //顯示標註的圖標  
  4. Drawable drawable = getResources().getDrawable(R.drawable.iconmarka);  
  5. mapView.getOverlays().add(new MyOverLayItem(drawable));  


資源所有類代碼

 

  1. package com.shuaiyin.baidu;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.graphics.Canvas;  
  7. import android.graphics.Paint;  
  8. import android.graphics.Point;  
  9. import android.graphics.drawable.Drawable;  
  10. import android.os.Bundle;  
  11. import android.widget.Toast;  
  12.   
  13. import com.baidu.mapapi.BMapManager;  
  14. import com.baidu.mapapi.GeoPoint;  
  15. import com.baidu.mapapi.ItemizedOverlay;  
  16. import com.baidu.mapapi.MKGeneralListener;  
  17. import com.baidu.mapapi.MapActivity;  
  18. import com.baidu.mapapi.MapController;  
  19. import com.baidu.mapapi.MapView;  
  20. import com.baidu.mapapi.Overlay;  
  21. import com.baidu.mapapi.OverlayItem;  
  22. /** 
  23.  * 讓百度地圖繼承MapActivity 
  24.  * @author shuaiyin 
  25.  * 
  26.  */  
  27. public class BaiDu_SuYiActivity extends MapActivity {  
  28.       
  29.     //添加百度地圖的相關控件  
  30.     private MapView mapView;  
  31.     //加載百度地圖的引發  
  32.     private BMapManager bMapManager;  
  33.     //定義百度地圖的KEY  
  34.     private String key = "*我處理了*94B0429A4BEE30797E04D91B0211C4";  
  35.     //在百度地圖上添加相應的控件  
  36.     private MapController mapController;  
  37.       
  38.     @Override  
  39.     public void onCreate(Bundle savedInstanceState) {  
  40.         super.onCreate(savedInstanceState);  
  41.         setContentView(R.layout.main);  
  42.           
  43.         //首先實例化mapView  
  44.         mapView = (MapView) this.findViewById(R.id.bmapView);  
  45.         bMapManager = new BMapManager(BaiDu_SuYiActivity.this);  
  46.         //調用百度地圖加載KEY  
  47.         bMapManager.init(key, new MKGeneralListener() {  
  48.               
  49.             @Override  
  50.             public void onGetPermissionState(int arg0) {  
  51.                 if(arg0 == 300){  
  52.                     Toast.makeText(BaiDu_SuYiActivity.this"您輸入的KEY有問題,請覈實"2).show();  
  53.                 }  
  54.             }  
  55.               
  56.             @Override  
  57.             public void onGetNetworkState(int arg0) {  
  58.                   
  59.             }  
  60.         });  
  61.           
  62.         this.initMapActivity(bMapManager);  
  63.         //表示能夠設置縮放功能  
  64.         mapView.setBuiltInZoomControls(true);  
  65.         mapController = mapView.getController();  
  66.           
  67.         //V1.00  
  68.         //在百度地圖上標註一箇中心點  
  69.         //GeoPoint geoPoint = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));  
  70.         //給地圖對象設置一箇中心點  
  71.         //mapController.setCenter(geoPoint);  
  72.         //設置地圖的縮放級別  
  73.         //mapController.setZoom(12);  
  74.         //顯示交通地圖  
  75.         //mapView.setTraffic(true);  
  76.         //顯示衛星地圖  
  77.         //mapView.setSatellite(true);  
  78.           
  79.         //v2.00  
  80.         //給地圖對象設置標註物  
  81.         //mapView.getOverlays().add(new MyOverlay());  
  82.         //mapController.setZoom(12);  
  83.           
  84.         //v3.00  
  85.         //給地圖設置多個標註物  
  86.         //顯示標註的圖標  
  87.         Drawable drawable = getResources().getDrawable(R.drawable.iconmarka);  
  88.         mapView.getOverlays().add(new MyOverLayItem(drawable));  
  89.     }  
  90.       
  91.     //標註一個遮蓋物  
  92.     public class MyOverlay extends Overlay{  
  93.         //聲明一個地點  
  94.         private GeoPoint geoPoint = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));  
  95.         //聲明一個畫筆工具  
  96.         private Paint paint = new Paint();  
  97.           
  98.         @Override  
  99.         public void draw(Canvas arg0, MapView arg1, boolean arg2) {  
  100.             super.draw(arg0, arg1, arg2);  
  101.               
  102.             Point point = mapView.getProjection().toPixels(geoPoint, null);  
  103.             arg0.drawText("這裏是天安門", point.x,point.y,paint);  
  104.         }  
  105.     }  
  106.       
  107.     //標註多個遮蓋物  
  108.     public class MyOverLayItem extends ItemizedOverlay<OverlayItem>{  
  109.           
  110.         private List<OverlayItem> overlayItem = new ArrayList<OverlayItem>();  
  111.           
  112.         //定義一組座標  
  113.         private double mLat1 = 39.90923;//經  
  114.         private double mLot1 = 116.397428;//緯  
  115.           
  116.         private double mLat2 = 39.92923;//經  
  117.         private double mLot2 = 116.377428;//緯  
  118.           
  119.         private double mLat3 = 39.94923;//經  
  120.         private double mLot3 = 116.357428;//緯  
  121.           
  122.         private double mLat4 = 39.96923;//經  
  123.         private double mLot4 = 116.337428;//緯  
  124.           
  125.         //用於在地圖上標識座標,用一個圖片標註  
  126.         public MyOverLayItem(Drawable drawable) {  
  127.             super(drawable);  
  128.             GeoPoint geoPoint1 = new GeoPoint((int)(mLat1*1E6),(int)(mLot1*1E6));  
  129.             GeoPoint geoPoint2 = new GeoPoint((int)(mLat2*1E6),(int)(mLot2*1E6));  
  130.             GeoPoint geoPoint3 = new GeoPoint((int)(mLat3*1E6),(int)(mLot3*1E6));  
  131.             GeoPoint geoPoint4 = new GeoPoint((int)(mLat4*1E6),(int)(mLot4*1E6));  
  132.               
  133.             overlayItem.add(new OverlayItem(geoPoint1, "A""這是第一個"));  
  134.             overlayItem.add(new OverlayItem(geoPoint2, "B""這是第二個"));  
  135.             overlayItem.add(new OverlayItem(geoPoint3, "C""這是第三個"));  
  136.             overlayItem.add(new OverlayItem(geoPoint4, "D""這是第四個"));  
  137.             //刷新地圖  
  138.             populate();  
  139.         }  
  140.           
  141.         //返回指定的List集合中每個座標  
  142.         @Override  
  143.         protected OverlayItem createItem(int arg0) {  
  144.             return overlayItem.get(arg0);  
  145.         }  
  146.   
  147.         @Override  
  148.         public int size() {  
  149.             return overlayItem.size();  
  150.         }  
  151.           
  152.         //當標註物被點擊的時候  
  153.         @Override  
  154.         public boolean onTap(int i) {  
  155.             Toast.makeText(BaiDu_SuYiActivity.this, overlayItem.get(i).getSnippet(), 2).show();  
  156.             return true;  
  157.         }  
  158.     }  
  159.       
  160.       
  161.       
  162.     @Override  
  163.     protected void onDestroy() {  
  164.         super.onDestroy();  
  165.         if(bMapManager != null){  
  166.             bMapManager.destroy();  
  167.             bMapManager = null;  
  168.         }  
  169.     }  
  170.     @Override  
  171.     protected void onResume() {  
  172.         super.onResume();  
  173.         if(bMapManager != null){  
  174.             bMapManager.start();  
  175.         }  
  176.     }  
  177.     @Override  
  178.     protected void onPause() {  
  179.         super.onPause();  
  180.         if(bMapManager != null){  
  181.             bMapManager.stop();  
  182.         }  
  183.     }  
  184.       
  185.     @Override  
  186.     protected boolean isRouteDisplayed() {  
  187.         return false;  
  188.     }  
  189. }  


最後看看上面四個東西的效果圖


但願你們在看了個人博客後,能跟我一塊兒進步,你們加油,好好學習,每天向上。

相關文章
相關標籤/搜索