在上篇文章中给你们简单的搭建了百度地图开发的基本环境,今天给你们来介绍介绍如何在地图上面添加标注物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;
- }
- }
最后看看上面四个东西的效果图




但愿你们在看了个人博客后,能跟我一块儿进步,你们加油,好好学习,每天向上。