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 = BitmapDescriptorFactory 108 .fromResource(R.drawable.startpoint); 109 // 构建MarkerOption,用于在地图上添加Marker 110 OverlayOptions option = new MarkerOptions().position(point) 111 .icon(bitmap); 112 // 在地图上添加Marker,并显示 113 mBaiduMap.addOverlay(option); 114 115 MapStatusUpdate mapstatusupdate = MapStatusUpdateFactory 116 .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 @Override 129 public void onGetWalkingRouteResult(WalkingRouteResult arg0) { 130 // 获取步行路线规划结果 131 } 132 133 @Override 134 public void onGetTransitRouteResult(TransitRouteResult arg0) { 135 // 获取公交换乘路径规划结果 136 } 137 138 @Override 139 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 str 157 */ 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 }
在地图上显示当前的位置
时间: 2024-12-11 09:35:27