zoukankan      html  css  js  c++  java
  • Android百度地图基础实现(标记+GPS) .

      1 package com.baidu.map;
      2 
      3 import java.util.ArrayList;
      4 import java.util.List;
      5 
      6 import android.content.Context;
      7 import android.graphics.Canvas;
      8 import android.graphics.Color;
      9 import android.graphics.Paint;
     10 import android.graphics.Point;
     11 import android.graphics.drawable.Drawable;
     12 import android.location.Location;
     13 import android.location.LocationManager;
     14 import android.os.Bundle;
     15 import android.widget.Toast;
     16 
     17 import com.baidu.mapapi.BMapManager;
     18 import com.baidu.mapapi.GeoPoint;
     19 import com.baidu.mapapi.ItemizedOverlay;
     20 import com.baidu.mapapi.LocationListener;
     21 import com.baidu.mapapi.MKAddrInfo;
     22 import com.baidu.mapapi.MKDrivingRouteResult;
     23 import com.baidu.mapapi.MKPoiResult;
     24 import com.baidu.mapapi.MKSearchListener;
     25 import com.baidu.mapapi.MKTransitRouteResult;
     26 import com.baidu.mapapi.MKWalkingRouteResult;
     27 import com.baidu.mapapi.MapActivity;
     28 import com.baidu.mapapi.MapController;
     29 import com.baidu.mapapi.MapView;
     30 import com.baidu.mapapi.MyLocationOverlay;
     31 import com.baidu.mapapi.OverlayItem;
     32 import com.baidu.mapapi.PoiOverlay;
     33 import com.baidu.mapapi.Projection;
     34 
     35 public class BmapActivity extends MapActivity {
     36     // 定义地图引擎管理类
     37     private BMapManager mapManager;// 定义搜索服务类
     38     private MapView mapView;
     39     private MapController mapController;
     40 
     41     LocationListener mLocationListener = null;// onResume时注册此listener,onPause时需要Remove
     42     MyLocationOverlay mLocationOverlay = null; // 定位图层
     43     private double dLat;
     44     private double dLon;
     45 
     46     @Override
     47     public void onCreate(Bundle savedInstanceState) {
     48         super.onCreate(savedInstanceState);
     49         setContentView(R.layout.main);
     50 
     51         
     52         // 初始化MapActivity
     53         mapManager = new BMapManager(getApplication());
     54 
     55         // init方法的第一个参数需填入申请的APIKey
     56         mapManager.init("DE7BC8B377AFA895ED0C16F504B5C4FC5E3E149B", null);
     57         super.initMapActivity(mapManager);
     58         mapView = (MapView) findViewById(R.id.map_View);
     59         // 设置地图模式为交通地图
     60         // mapView.setTraffic(true);
     61         // 设置启用内置的缩放控件
     62         mapView.setBuiltInZoomControls(true);
     63         mapView.displayZoomControls(true);
     64         // 设置在缩放动画过程中也显示overlay,默认为不绘制
     65         mapView.setDrawOverlayWhenZooming(true);
     66          mapController = mapView.getController(); 
     67          mapController.setZoom(16);
     68          
     69          //定位自己的位置
     70          myself();
     71          
     72         // 添加定位图层
     73         mLocationOverlay = new MyLocationOverlay(this, mapView);
     74         mapView.getOverlays().add(mLocationOverlay);
     75 
     76         Drawable marker = getResources().getDrawable(R.drawable.da_marker_red);
     77         marker.setBounds(0, 0, marker.getIntrinsicWidth(),
     78                 marker.getIntrinsicHeight());// Intrinsic固有
     79         mapView.getOverlays().add(new MyItemizedOverlay(marker, this));
     80         
     81     }
     82 
     83     // 同一类型覆盖物的绘制
     84     class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
     85         // 属性
     86         private Drawable marker;
     87         private Context mContext;
     88         private List<OverlayItem> geoList = new ArrayList<OverlayItem>();
     89 
     90         // 构造方法
     91         public MyItemizedOverlay(Drawable marker, Context context) {
     92             super(boundCenterBottom(marker));
     93 
     94             this.marker = marker;
     95             this.mContext = context;
     96 
     97             // 构造地理坐标
     98             GeoPoint p1 = new GeoPoint((int) (dLat * 1E6), (int) (dLon * 1E6));
     99             geoList.add(new OverlayItem(p1, "P1", "这是我的当前位置"));
    100 
    101             populate();// 执行填充方法
    102 
    103         }
    104 
    105         // 绘制方法
    106         public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    107             // 投影,用于屏幕像素点坐标系统与地球经纬度点坐标系统的转换
    108             Projection projection = mapView.getProjection();
    109             for (int index = size() - 1; index >= 0; index--) {
    110                 OverlayItem overlayItem = this.getItem(index);
    111                 String title = overlayItem.getTitle();
    112                 Point point = projection.toPixels(overlayItem.getPoint(), null);
    113 
    114                 Paint painttext = new Paint();
    115                 painttext.setColor(Color.BLACK);
    116                 painttext.setTextSize(15);
    117                 canvas.drawText(title, point.x - 30, point.y - 25, painttext);
    118 
    119             }
    120 
    121             super.draw(canvas, mapView, shadow);
    122             boundCenterBottom(marker);
    123 
    124         }
    125 
    126         // 添加成员方法
    127         @Override
    128         protected OverlayItem createItem(int i) {
    129 
    130             return geoList.get(i);
    131         }
    132 
    133         @Override
    134         public int size() {
    135 
    136             return geoList.size();
    137         }
    138 
    139         // 添加点击事件
    140         public boolean onTap(int i) {
    141             setFocus(geoList.get(i));
    142             Toast.makeText(this.mContext, geoList.get(i).getSnippet(),
    143                     Toast.LENGTH_LONG).show();// snippet片段
    144             return true;
    145         }
    146 
    147         public boolean onTap(GeoPoint point, MapView mapView) {
    148             return super.onTap(point, mapView);
    149         }
    150 
    151     }
    152 
    153     // 定位到自己的位置
    154     public void myself() {
    155         LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    156         Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    157         if (l == null) {
    158             Toast.makeText(BmapActivity.this, "无法获取自己的位置", Toast.LENGTH_SHORT)
    159                     .show();
    160             /* 默认 的位置 */
    161             dLat = 34.824289;
    162             dLon = 113.689044;
    163              GeoPoint geoPoint = new GeoPoint((int) (dLat * 1e6), (int) (dLon * 1e6)); 
    164              mapController.setCenter(geoPoint);
    165 
    166         } else {
    167 
    168             dLat = (int) (l.getLatitude() * 1E6);
    169             dLon = (int) (l.getLongitude() * 1E6);
    170         }
    171     } 
    172 
    173     @Override
    174     protected boolean isRouteDisplayed() {
    175         return false;
    176     }
    177 
    178     @Override
    179     protected void onDestroy() {
    180         if (mapManager != null) {
    181             // 程序退出前需调用此方法
    182             mapManager.destroy();
    183             mapManager = null;
    184         }
    185         super.onDestroy();
    186     }
    187 
    188     @Override
    189     protected void onPause() {
    190         if (mapManager != null) {
    191             // 终止百度地图API
    192             mapManager.getLocationManager().removeUpdates(mLocationListener);
    193             mLocationOverlay.disableMyLocation();
    194             mLocationOverlay.disableCompass(); // 关闭指南针
    195             mapManager.stop();
    196         }
    197         super.onPause();
    198     }
    199 
    200     @Override
    201     protected void onResume() {
    202         if (mapManager != null) {
    203             // 开启百度地图API
    204             // 注册定位事件,定位后将地图移动到定位点
    205             mapManager.getLocationManager().requestLocationUpdates(
    206                     mLocationListener);
    207             mLocationOverlay.enableMyLocation();
    208             mLocationOverlay.enableCompass(); // 打开指南针
    209             mapManager.start();
    210         }
    211         super.onResume();
    212     }
    213 
    214     /**
    215      * 实现MKSearchListener接口,用于实现异步搜索服务
    216      * 
    217      * @author liufeng
    218      */
    219     public class MySearchListener implements MKSearchListener {
    220         /**
    221          * 
    222          * 根据经纬度搜索地址信息结果
    223          * 
    224          * @param result
    225          *            搜索结果
    226          * @param iError
    227          *            错误号 (0表示正确返回)
    228          */
    229         public void onGetAddrResult(MKAddrInfo result, int iError) {
    230         }
    231 
    232         /**
    233          * 驾车路线搜索结果
    234          * 
    235          * @param result
    236          *            搜索结果
    237          * @param iError
    238          *            错误号
    239          */
    240         public void onGetDrivingRouteResult(MKDrivingRouteResult result,
    241                 int iError) {
    242         }
    243 
    244         /**
    245          * * POI搜索结果(范围检索、城市POI检索、周边检索) * @param result 搜索结果
    246          * 
    247          * @param type
    248          *            返回结果类型(11,12,21:poi列表 7:城市列表)
    249          * @param iError
    250          *            错误号(0表示正确返回)
    251          */
    252         public void onGetPoiResult(MKPoiResult result, int type, int iError) {
    253             if (result == null) {
    254                 return;
    255             }
    256             // PoiOverlay是baidu map api提供的用于显示POI的Overlay
    257             PoiOverlay poioverlay = new PoiOverlay(BmapActivity.this, mapView);
    258             // 设置搜索到的POI数据
    259             poioverlay.setData(result.getAllPoi());
    260             // 在地图上显示PoiOverlay(将搜索到的兴趣点标注在地图上)
    261             mapView.getOverlays().add(poioverlay);
    262         }
    263 
    264         /**
    265          * 公交换乘路线搜索结果 * * @param result 搜索结果 *
    266          * 
    267          * @param iError
    268          *            错误号(0表示正确返回)
    269          * */
    270         public void onGetTransitRouteResult(MKTransitRouteResult result,
    271                 int iError) {
    272         }
    273 
    274         /**
    275          * * 步行路线搜索结果 *
    276          * 
    277          * @param result
    278          *            搜索结果 *
    279          * @param iError
    280          *            错误号(0表示正确返回)
    281          * */
    282         public void onGetWalkingRouteResult(MKWalkingRouteResult result,
    283                 int iError) {
    284         }
    285     }
    286 }
  • 相关阅读:
    L05 Laravel 教程 电商实战
    laravel 5.5 登录验证码 captcha 引入
    thinkphp 清理runtime缓存的方法, 清理指定目录
    艾伟也谈项目管理,项目经理要如何看待技术? 狼人:
    艾伟也谈项目管理,带领团队发挥最大潜能的10个技巧 狼人:
    艾伟也谈项目管理,聊聊我们团队的绩效管理 狼人:
    艾伟也谈项目管理,创建敏捷团队 狼人:
    艾伟也谈项目管理,多任务让你走得更慢 狼人:
    艾伟也谈项目管理,项目经理的思维批判 狼人:
    艾伟也谈项目管理,创业公司技术选型参考 狼人:
  • 原文地址:https://www.cnblogs.com/ggzjj/p/2856238.html
Copyright © 2011-2022 走看看