复制代码
1根据经纬度显示位置和地址
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228public class LocationFragment extends BaseFragment implements View.OnClickListener, OnGetGeoCoderResultListener { @Override public View onCreateViewIfNull(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Intent intent = this.getActivityIntent(); if (intent != null) { Bundle bundle = intent.getBundleExtra(KeyContants.ACTIVITYBUNDLE); if (bundle != null) { double latitude = bundle.getDouble(KeyContants.LATITUDE); double longitude = bundle.getDouble(KeyContants.LONGITUDE); if (latitude > 0 && longitude > 0) { shopPoint = new LatLng(latitude, longitude); } shopAddress =bundle.getString(KeyContants.ADDRESSS); } } mParentView = inflater.inflate(R.layout.zeno_frag_location, null); ViewUtils.inject(this, mParentView); initView(); initData(); initListener(); return mParentView; } @Override public TitleBarInfo onCreateTitleBar() { TitleBarInfo info = Factory.getInstance().getTitleResManager().createStandardWithBack(getActivity()); info.setTitle("店铺位置"); return info; } private void initView() { tvShopAddress = (TextView) View.inflate(getActivity(), R.layout.zeno_layout_address_marker, null); BaiduMapOptions mapOptions = new BaiduMapOptions().compassEnabled(false).zoomControlsEnabled(true).rotateGesturesEnabled(false); mMapView = new MapView(getActivity(), mapOptions); mRLMap.addView(mMapView, 0); // 获取当前定位城市 GeoPoint gp = Factory.getInstance().getGeoMap().getGP(); myPoint = new LatLng(gp.getLatitude(), gp.getLongitude()); mGeoCoder = GeoCoder.newInstance(); mGeoCoder.setOnGetGeoCodeResultListener(this); mBaiduMap = mMapView.getMap(); mBaiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() { @Override public void onMapLoaded() { //地图加载完成后再设置Bound hasLoaded=true; Logger.e("leon","onMapLoaded"); //调整缩放比例,将定位和店铺坐标同时显示 LatLngBounds.Builder builder = new LatLngBounds.Builder(); if(myPoint!=null && shopPoint!=null){ Logger.e("leon","onMapLoaded mypoint:"+myPoint.latitude+":"+myPoint.longitude+" shop point:"+shopPoint.latitude+":"+shopPoint.longitude); builder.include(myPoint).include(shopPoint); mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLngBounds(builder.build())); Logger.e("leon","onMapLoaded zoom:"+mBaiduMap.getMapStatus().zoom); } } }); // // 设置缩放级别 MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(17.5f); mBaiduMap.setMapStatus(msu); //显示定位图标 // 修改为自定义marker mCurrentMarker = BitmapDescriptorFactory .fromResource(R.drawable.zeno_ic_map_center_location); mBaiduMap .setMyLocationConfigeration(new MyLocationConfiguration( mCurrentMode, true, mCurrentMarker)); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); if (myPoint != null && myPoint.latitude > 0 && myPoint.longitude > 0) { setMyLocation(myPoint); } else {//重新获取定位坐标 mLocClient = new LocationClient(getActivity()); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setScanSpan(5000); mLocClient.setLocOption(option); mLocClient.start(); } LatLng point = DEFAULT_LOCATION; if (shopPoint != null && shopPoint.latitude > 0 && shopPoint.longitude > 0) { point = shopPoint; //有店铺经纬度,则去取详细地址,并显示在地图上 mGeoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(shopPoint)); } else { MsgToast.geToast().setLongMsg("该店铺尚未设置地图位置"); if (myPoint != null && myPoint.latitude > 0 && myPoint.longitude > 0) { //没有店铺经纬度,显示当前定位 point = myPoint; } moveTo(point); } } private void setMyLocation(LatLng point) { MyLocationData locData = new MyLocationData.Builder() .latitude(point.latitude) .longitude(point.longitude).build(); mBaiduMap.setMyLocationData(locData); } private void initData() { } private void initListener() { mIVMyLocation.setOnClickListener(this); } @Override public void onClick(View v) { if (mIVMyLocation == v) { //跳转到定位 if (myPoint != null) { moveTo(myPoint); } else { if (mLocClient != null) { mLocClient.start(); } } } } private void moveTo(LatLng point) { MapStatus.Builder builder = new MapStatus.Builder(); builder.target(point); mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build())); } @Override public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) { } @Override public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) { if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { // 没有找到 moveTo(myPoint); return; } try { //找到了则移动到店铺地址 String address = result.getAddress(); //在地图上添加图标 tvShopAddress.setText(!TextUtils.isEmpty(shopAddress)?shopAddress:address); MarkerOptions shopMarker = new MarkerOptions().position(result.getLocation()).icon(BitmapDescriptorFactory.fromView(tvShopAddress)); mBaiduMap.addOverlay(shopMarker); } catch (Exception e) { e.printStackTrace(); } } /** * 定位SDK监听函数 */ public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // map view 销毁后不在处理新接收的位置 if (location == null || mMapView == null) { return; } myPoint = new LatLng(location.getLatitude(), location.getLongitude()); setMyLocation(myPoint); //定位成功,停止定位 if(mLocClient != null){ mLocClient.stop(); } } public void onReceivePoi(BDLocation poiLocation) { } } @Override public void onPause() { // MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause() if (mMapView != null) { mMapView.onPause(); } super.onPause(); } @Override public void onResume() { // MapView的生命周期与Activity同步,当activity恢复时需调用MapView.onResume() if (mMapView != null) { mMapView.onResume(); } super.onResume(); } @Override public void onDestroy() { try { if (mGeoCoder != null) { mGeoCoder.destroy(); mGeoCoder = null; } // 退出时销毁定位 if(mLocClient!=null){ mLocClient.stop(); } // MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy() if (mMapView != null) { mMapView.onDestroy(); mMapView = null; } } catch (Exception e) { e.printStackTrace(); } super.onDestroy(); } /** * 默认位置,公司坐标 */ private static final LatLng DEFAULT_LOCATION = new LatLng(22.518143, 113.930614); private View mParentView; private MapView mMapView = null; @ViewInject(R.id.zeno_mRLMap) private RelativeLayout mRLMap; private BaiduMap mBaiduMap = null; private LatLng myPoint;//当前定位 private LatLng shopPoint;//店铺位置 private String shopAddress; private TextView tvShopAddress; /** * 根据地理坐标查找地址 */ private GeoCoder mGeoCoder = null; // 定位相关 @ViewInject(R.id.mIVMyLocation) private ImageView mIVMyLocation; private LocationClient mLocClient; public MyLocationListenner myListener = new MyLocationListenner(); private MyLocationConfiguration.LocationMode mCurrentMode; private BitmapDescriptor mCurrentMarker; private boolean hasLoaded=false; }
最后
以上就是鲜艳钥匙最近收集整理的关于调用百度地图SDK显示当前定位位置的全部内容,更多相关调用百度地图SDK显示当前定位位置内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复