在上篇文章中給你們簡單的搭建了百度地圖開發的基本環境,今天給你們來介紹介紹如何在地圖上面添加標註物java
如對這篇文章有看不懂的地方,請轉戰到上一篇文章-->飛機直達android
在正式開始以前先請你們注意,在轉載博客的時候注意說明出處
api
今天給你們帶來四個方面的結束,ide
第一個:就是介紹地圖顯示交通訊息咱們只須要添加一個代碼就能夠完成工具
- mapView.setTraffic(true);
第二個:給地圖顯示衛星地圖,一樣也很簡單,一樣只須要一句代碼學習
- mapView.setSatellite(true);
第三個:給地圖設置一個標註物ui
這裏用到的是百度地圖提供的 Overlay 對象 --> 移步百度地圖官方APIthis
首先寫一個內部類繼承自 Overlay 對象spa
-
- public class MyOverlay extends Overlay{
-
- private GeoPoint geoPoint = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));
-
- private Paint paint = new Paint();
-
- @Override
- public void draw(Canvas arg0, MapView arg1, boolean arg2) {
- super.draw(arg0, arg1, arg2);
-
- Point point = mapView.getProjection().toPixels(geoPoint, null);
- arg0.drawText("這裏是天安門", point.x,point.y,paint);
- }
- }
而後在onCreate方法中對該類進行調用.net
這樣就能夠很簡單的使用標註物啦。
第四個:是如何給百度地圖設置多個標註物一樣的道理這裏咱們用到的是百度地圖API裏面提供的 ItemizedOverlay 對象
一樣寫一個內部類繼承自 ItemizedOverlay 對象
-
- public class MyOverLayItem extends ItemizedOverlay<OverlayItem>{
-
- private List<OverlayItem> overlayItem = new ArrayList<OverlayItem>();
-
-
- private double mLat1 = 39.90923;
- private double mLot1 = 116.397428;
-
- private double mLat2 = 39.92923;
- private double mLot2 = 116.377428;
-
- private double mLat3 = 39.94923;
- private double mLot3 = 116.357428;
-
- private double mLat4 = 39.96923;
- private double mLot4 = 116.337428;
-
-
- public MyOverLayItem(Drawable drawable) {
- super(drawable);
- GeoPoint geoPoint1 = new GeoPoint((int)(mLat1*1E6),(int)(mLot1*1E6));
- GeoPoint geoPoint2 = new GeoPoint((int)(mLat2*1E6),(int)(mLot2*1E6));
- GeoPoint geoPoint3 = new GeoPoint((int)(mLat3*1E6),(int)(mLot3*1E6));
- GeoPoint geoPoint4 = new GeoPoint((int)(mLat4*1E6),(int)(mLot4*1E6));
-
- overlayItem.add(new OverlayItem(geoPoint1, "A", "這是第一個"));
- overlayItem.add(new OverlayItem(geoPoint2, "B", "這是第二個"));
- overlayItem.add(new OverlayItem(geoPoint3, "C", "這是第三個"));
- overlayItem.add(new OverlayItem(geoPoint4, "D", "這是第四個"));
-
- populate();
- }
-
-
- @Override
- protected OverlayItem createItem(int arg0) {
- return overlayItem.get(arg0);
- }
-
- @Override
- public int size() {
- return overlayItem.size();
- }
-
-
- @Override
- public boolean onTap(int i) {
- Toast.makeText(BaiDu_SuYiActivity.this, overlayItem.get(i).getSnippet(), 2).show();
- return true;
- }
- }
而後在onCreate方法中對該類進行調用
-
-
-
- Drawable drawable = getResources().getDrawable(R.drawable.iconmarka);
- mapView.getOverlays().add(new MyOverLayItem(drawable));
資源所有類代碼
- package com.shuaiyin.baidu;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Point;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.widget.Toast;
-
- import com.baidu.mapapi.BMapManager;
- import com.baidu.mapapi.GeoPoint;
- import com.baidu.mapapi.ItemizedOverlay;
- import com.baidu.mapapi.MKGeneralListener;
- import com.baidu.mapapi.MapActivity;
- import com.baidu.mapapi.MapController;
- import com.baidu.mapapi.MapView;
- import com.baidu.mapapi.Overlay;
- import com.baidu.mapapi.OverlayItem;
-
-
-
-
-
- public class BaiDu_SuYiActivity extends MapActivity {
-
-
- private MapView mapView;
-
- private BMapManager bMapManager;
-
- private String key = "*我處理了*94B0429A4BEE30797E04D91B0211C4";
-
- private MapController mapController;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
-
- mapView = (MapView) this.findViewById(R.id.bmapView);
- bMapManager = new BMapManager(BaiDu_SuYiActivity.this);
-
- bMapManager.init(key, new MKGeneralListener() {
-
- @Override
- public void onGetPermissionState(int arg0) {
- if(arg0 == 300){
- Toast.makeText(BaiDu_SuYiActivity.this, "您輸入的KEY有問題,請覈實", 2).show();
- }
- }
-
- @Override
- public void onGetNetworkState(int arg0) {
-
- }
- });
-
- this.initMapActivity(bMapManager);
-
- mapView.setBuiltInZoomControls(true);
- mapController = mapView.getController();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Drawable drawable = getResources().getDrawable(R.drawable.iconmarka);
- mapView.getOverlays().add(new MyOverLayItem(drawable));
- }
-
-
- public class MyOverlay extends Overlay{
-
- private GeoPoint geoPoint = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));
-
- private Paint paint = new Paint();
-
- @Override
- public void draw(Canvas arg0, MapView arg1, boolean arg2) {
- super.draw(arg0, arg1, arg2);
-
- Point point = mapView.getProjection().toPixels(geoPoint, null);
- arg0.drawText("這裏是天安門", point.x,point.y,paint);
- }
- }
-
-
- public class MyOverLayItem extends ItemizedOverlay<OverlayItem>{
-
- private List<OverlayItem> overlayItem = new ArrayList<OverlayItem>();
-
-
- private double mLat1 = 39.90923;
- private double mLot1 = 116.397428;
-
- private double mLat2 = 39.92923;
- private double mLot2 = 116.377428;
-
- private double mLat3 = 39.94923;
- private double mLot3 = 116.357428;
-
- private double mLat4 = 39.96923;
- private double mLot4 = 116.337428;
-
-
- public MyOverLayItem(Drawable drawable) {
- super(drawable);
- GeoPoint geoPoint1 = new GeoPoint((int)(mLat1*1E6),(int)(mLot1*1E6));
- GeoPoint geoPoint2 = new GeoPoint((int)(mLat2*1E6),(int)(mLot2*1E6));
- GeoPoint geoPoint3 = new GeoPoint((int)(mLat3*1E6),(int)(mLot3*1E6));
- GeoPoint geoPoint4 = new GeoPoint((int)(mLat4*1E6),(int)(mLot4*1E6));
-
- overlayItem.add(new OverlayItem(geoPoint1, "A", "這是第一個"));
- overlayItem.add(new OverlayItem(geoPoint2, "B", "這是第二個"));
- overlayItem.add(new OverlayItem(geoPoint3, "C", "這是第三個"));
- overlayItem.add(new OverlayItem(geoPoint4, "D", "這是第四個"));
-
- populate();
- }
-
-
- @Override
- protected OverlayItem createItem(int arg0) {
- return overlayItem.get(arg0);
- }
-
- @Override
- public int size() {
- return overlayItem.size();
- }
-
-
- @Override
- public boolean onTap(int i) {
- Toast.makeText(BaiDu_SuYiActivity.this, overlayItem.get(i).getSnippet(), 2).show();
- return true;
- }
- }
-
-
-
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if(bMapManager != null){
- bMapManager.destroy();
- bMapManager = null;
- }
- }
- @Override
- protected void onResume() {
- super.onResume();
- if(bMapManager != null){
- bMapManager.start();
- }
- }
- @Override
- protected void onPause() {
- super.onPause();
- if(bMapManager != null){
- bMapManager.stop();
- }
- }
-
- @Override
- protected boolean isRouteDisplayed() {
- return false;
- }
- }
最後看看上面四個東西的效果圖




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