android:百度地图-给地图添加标注物
在上篇文章中给大家简单的搭建了百度地图开发的基本环境,今天给大家来介绍介绍如何在地图上面添加标注物
如对这篇文章有看不懂的地方,请转战到上一篇文章-->飞机直达
在正式开始之前先请大家注意,在转载博客的时候注意说明出处
今天给大家带来四个方面的结束,
第一个:就是介绍地图显示交通信息我们只需要添加一个代码就可以完成
- mapView.setTraffic(true);
第二个:给地图显示卫星地图,同样也很简单,同样只需要一句代码
- mapView.setSatellite(true);
第三个:给地图设置一个标注物
这里用到的是百度地图提供的 Overlay 对象 --> 移步百度地图官方API
首先写一个内部类继承自 Overlay 对象
- //标注一个遮盖物
- 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方法中对该类进行调用
- //v2.00
- //给地图对象设置标注物
- //mapView.getOverlays().add(new MyOverlay());
- //mapController.setZoom(12);
这样就可以很简单的使用标注物啦。
第四个:是如何给百度地图设置多个标注物同样的道理这里我们用到的是百度地图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();
- }
- //返回指定的List集合中每一个坐标
- @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方法中对该类进行调用
- //v3.00
- //给地图设置多个标注物
- //显示标注的图标
- 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;
- /**
- * 让百度地图继承MapActivity
- * @author shuaiyin
- *
- */
- public class BaiDu_SuYiActivity extends MapActivity {
- //添加百度地图的相关控件
- private MapView mapView;
- //加载百度地图的引起
- private BMapManager bMapManager;
- //定义百度地图的KEY
- private String key = "*我处理了*94B0429A4BEE30797E04D91B0211C4";
- //在百度地图上添加相应的控件
- private MapController mapController;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //首先实例化mapView
- mapView = (MapView) this.findViewById(R.id.bmapView);
- bMapManager = new BMapManager(BaiDu_SuYiActivity.this);
- //调用百度地图加载KEY
- 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();
- //V1.00
- //在百度地图上标注一个中心点
- //GeoPoint geoPoint = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));
- //给地图对象设置一个中心点
- //mapController.setCenter(geoPoint);
- //设置地图的缩放级别
- //mapController.setZoom(12);
- //显示交通地图
- //mapView.setTraffic(true);
- //显示卫星地图
- //mapView.setSatellite(true);
- //v2.00
- //给地图对象设置标注物
- //mapView.getOverlays().add(new MyOverlay());
- //mapController.setZoom(12);
- //v3.00
- //给地图设置多个标注物
- //显示标注的图标
- 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();
- }
- //返回指定的List集合中每一个坐标
- @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;
- }
- }
最后看看上面四个东西的效果图
希望大家在看了我的博客后,能跟我一起进步,大家加油,好好学习,天天向上。
posted on 2013-07-19 11:59 clarenceV1 阅读(4957) 评论(0) 编辑 收藏 举报