博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在地图上显示当前的位置
阅读量:4973 次
发布时间:2019-06-12

本文共 6386 字,大约阅读时间需要 21 分钟。

1 package com.aihunqin.crazy;  2   3 import android.app.Activity;  4 import android.os.Bundle;  5 import android.view.View;  6 import android.view.View.OnClickListener;  7 import android.widget.TextView;  8   9 import com.baidu.location.BDLocation; 10 import com.baidu.location.BDLocationListener; 11 import com.baidu.location.LocationClient; 12 import com.baidu.location.LocationClientOption; 13 import com.baidu.location.LocationClientOption.LocationMode; 14 import com.baidu.mapapi.SDKInitializer; 15 import com.baidu.mapapi.map.BaiduMap; 16 import com.baidu.mapapi.map.BitmapDescriptor; 17 import com.baidu.mapapi.map.BitmapDescriptorFactory; 18 import com.baidu.mapapi.map.MapStatusUpdate; 19 import com.baidu.mapapi.map.MapStatusUpdateFactory; 20 import com.baidu.mapapi.map.MapView; 21 import com.baidu.mapapi.map.MarkerOptions; 22 import com.baidu.mapapi.map.OverlayOptions; 23 import com.baidu.mapapi.model.LatLng; 24 import com.baidu.mapapi.search.route.DrivingRoutePlanOption; 25 import com.baidu.mapapi.search.route.DrivingRouteResult; 26 import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener; 27 import com.baidu.mapapi.search.route.PlanNode; 28 import com.baidu.mapapi.search.route.RoutePlanSearch; 29 import com.baidu.mapapi.search.route.TransitRouteResult; 30 import com.baidu.mapapi.search.route.WalkingRouteResult; 31 import com.example.aihunqin.R; 32  33 public class WenhongLocation extends Activity { 34     BaiduMap mBaiduMap = null; 35     MapView mMapView = null; 36     public LocationClient mLocationClient; 37     public MyLocationListener mMyLocationListener; 38     public TextView mLocationResult, back; 39  40     @Override 41     protected void onCreate(Bundle savedInstanceState) { 42  43         super.onCreate(savedInstanceState); 44         SDKInitializer.initialize(getApplicationContext()); 45         setContentView(R.layout.layout_wenhonglocation); 46  47         back = (TextView) findViewById(R.id.back); 48         back.setVisibility(View.VISIBLE); 49         back.setOnClickListener(new OnClickListener() { 50  51             @Override 52             public void onClick(View v) { 53                 finish(); 54             } 55         }); 56  57         mMapView = (MapView) findViewById(R.id.bmapView); 58         mBaiduMap = mMapView.getMap(); 59  60         mLocationClient = new LocationClient(getApplicationContext()); 61         mMyLocationListener = new MyLocationListener(); 62         mLocationClient.registerLocationListener(mMyLocationListener); 63         InitLocation(); 64         mLocationClient.start(); 65  66     } 67  68     /** 69      * 实现定位回调监听 70      *  71      * @author Alex 72      *  73      */ 74     public class MyLocationListener implements BDLocationListener { 75  76         @Override 77         public void onReceiveLocation(BDLocation location) { 78             // Receive Location 79             StringBuffer sb = new StringBuffer(256); 80             sb.append("time:"); 81             sb.append(location.getTime()); 82             sb.append("\nerror code:"); 83             sb.append(location.getLocType()); 84             sb.append("\nlatitude:"); 85             sb.append(location.getLatitude()); 86             sb.append("\nlontitude:"); 87             sb.append(location.getLongitude()); 88             sb.append("\nradius:"); 89             sb.append(location.getRadius()); 90             if (location.getLocType() == BDLocation.TypeNetWorkLocation) { 91                 sb.append("\naddr:"); 92                 sb.append(location.getAddrStr()); 93                 // 运营商信息 94                 sb.append("\noperationers:"); 95                 sb.append(location.getOperators()); 96             } 97  98             setMarker(location.getLatitude(), location.getLongitude()); 99             // logMsg(sb.toString());100         }101     }102 103     void setMarker(double lat, double lng) {104         // 定义Markker坐标点105         LatLng point = new LatLng(lat, lng);106         // 构建Marker图标107         BitmapDescriptor bitmap = BitmapDescriptorFactory108                 .fromResource(R.drawable.startpoint);109         // 构建MarkerOption,用于在地图上添加Marker110         OverlayOptions option = new MarkerOptions().position(point)111                 .icon(bitmap);112         // 在地图上添加Marker,并显示113         mBaiduMap.addOverlay(option);114 115         MapStatusUpdate mapstatusupdate = MapStatusUpdateFactory116                 .newLatLng(point);117 118         mBaiduMap.setMapStatus(mapstatusupdate);119 120         // setRoutePlan(point);121     }122 123     void setRoutePlan(LatLng point) {124         // 驾车线路规划125         RoutePlanSearch mSearch = RoutePlanSearch.newInstance();126         OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() {127 128             @Override129             public void onGetWalkingRouteResult(WalkingRouteResult arg0) {130                 // 获取步行路线规划结果131             }132 133             @Override134             public void onGetTransitRouteResult(TransitRouteResult arg0) {135                 // 获取公交换乘路径规划结果136             }137 138             @Override139             public void onGetDrivingRouteResult(DrivingRouteResult arg0) {140                 // 获取驾车路线规划结果141             }142         };143         // 设置驾车线路规划检索监听者144         mSearch.setOnGetRoutePlanResultListener(listener);145         // 准备检索起、终点信息146         PlanNode stNode = PlanNode.withLocation(point);147         LatLng endpoint = new LatLng(121.473605, 31.232176);148         PlanNode enNode = PlanNode.withLocation(endpoint);149         mSearch.drivingSearch((new DrivingRoutePlanOption()).from(stNode).to(150                 enNode));151     }152 153     /**154      * 显示请求你字符串155      * 156      * @param str157      */158     public void logMsg(String str) {159         try {160             if (mLocationResult != null)161                 mLocationResult.setText(str);162         } catch (Exception e) {163             e.printStackTrace();164         }165     }166 167     private void InitLocation() {168         LocationClientOption option = new LocationClientOption();169         option.setLocationMode(LocationMode.Battery_Saving);// 设置定位模式170         option.setCoorType("bd09ll");171         option.setIsNeedAddress(true);172         mLocationClient.setLocOption(option);173     }174 }
View Code

 

转载于:https://www.cnblogs.com/daishuguang/p/3885159.html

你可能感兴趣的文章
CSS自学笔记(14):CSS3动画效果
查看>>
项目应用1
查看>>
Ubuntu下配置jdk和tomcat
查看>>
大型网站的演变升级
查看>>
图片延迟加载的实现
查看>>
php适配器模式(adapter pattern)
查看>>
C# 委托链(多播委托)
查看>>
解密个推持续集成
查看>>
基本SCTP套接字编程常用函数
查看>>
C 编译程序步骤
查看>>
页面抓取匹配时,万恶的\r,\n,\t 要先替换掉为空,出现匹配有问题,都是这个引起的...
查看>>
利用Node.js调用Elasticsearch
查看>>
构造函数
查看>>
LeetCode N-Queens
查看>>
jstat 命令
查看>>
leetcode[155]Min Stack
查看>>
《代码不朽:编写可维护软件的10大要则(C#版)》读后感
查看>>
04、我的.net Core的学习 - 网页版Hello World
查看>>
分块学习
查看>>
UIWebView 屏蔽或者修改 alert警告框
查看>>