百度地图点击地图显示所选位置与自定义回到定位按钮
2017-05-26 17:48
1331 查看
第一篇博客,,近期遇到一个关于地图选点显示地理位置与自定义回到定位按钮的需求, 以前做过,也是参考大神们的经验,之所以写出来就是为了把遇到的坑写一下,文笔不好,请谅解
百度地图api 貌似是没有显示这个定位按钮的,只能自己写,
布局
废话不说,自己看代码
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="你的百度key" />
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>
上面的service一定要记得写,不然无法实现返回自己位置的功能
最终效果
百度地图api 貌似是没有显示这个定位按钮的,只能自己写,
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:top="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:text="城市:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="佛山市" android:id="@+id/city_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:text="地址:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="佛山市南海桂城镇" android:id="@+id/address_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> <Button android:text="定位" android:id="@+id/dw_bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" android:layout_marginLeft="25dp" android:layout_marginBottom="10dip" /> </RelativeLayout> </LinearLayout>
废话不说,自己看代码
package com.example.baidumaptest; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.SDKInitializer; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BaiduMap.OnMapClickListener; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.BitmapDescriptorFactory; import com.baidu.mapapi.map.MapPoi; import com.baidu.mapapi.map.MapStatus; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.MarkerOptions; import com.baidu.mapapi.model.LatLng; import com.baidu.mapapi.search.geocode.GeoCodeResult; import com.baidu.mapapi.search.geocode.GeoCoder; import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener; import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption; import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult; public class BaiduTestActivity extends Activity { private MapView mMapView = null; private BaiduMap mBaiduMap; private BitmapDescriptor bitmap; private String address = ""; private double mCurrentLat = 0.0; private double mCurrentLon = 0.0; public MyLocationListenner myListener = new MyLocationListenner(); private LocationClient mLocClient; private TextView city_tv, address_tv; private Button dw_bt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 在使用SDK各组件之前初始化context信息,传入ApplicationContext // 注意该方法要再setContentView方法之前实现 SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_bdtest); city_tv = (TextView) findViewById(R.id.city_tv); address_tv = (TextView) findViewById(R.id.address_tv); dw_bt=(Button)findViewById(R.id.dw_bt); // 获取地图控件引用 mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); // 设置是否显示比例尺控件 mMapView.showScaleControl(false); // 设置是否显示缩放控件 mMapView.showZoomControls(true); // 设置是否显示定位按钮 // 删除百度地图LoGo mMapView.removeViewAt(1); // 设置marker图标 bitmap = BitmapDescriptorFactory .fromResource(R.drawable.ssdk_oks_ptr_ptr); mBaiduMap.setOnMapClickListener(new OnMapClickListener() { @Override public boolean onMapPoiClick(MapPoi arg0) { // TODO Auto-generated method stub return false; } // 此方法就是点击地图监听 @Override public void onMapClick(LatLng latLng) { // 获取经纬度 mCurrentLat = latLng.latitude; mCurrentLon = latLng.longitude; // 先清除图层 mBaiduMap.clear(); // 定义Maker坐标点 LatLng point = new LatLng(mCurrentLat, mCurrentLon); // 构建MarkerOption,用于在地图上添加Marker MarkerOptions options = new MarkerOptions().position(point) .icon(bitmap); // 在地图上添加Marker,并显示 mBaiduMap.addOverlay(options); // 实例化一个地理编码查询对象 GeoCoder geoCoder = GeoCoder.newInstance(); // 设置反地理编码位置坐标 ReverseGeoCodeOption op = new ReverseGeoCodeOption(); op.location(latLng); // 发起反地理编码请求(经纬度->地址信息) geoCoder.reverseGeoCode(op); geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() { @Override public void onGetReverseGeoCodeResult( ReverseGeoCodeResult arg0) { // 获取点击的坐标地址 address = arg0.getAddress(); city_tv.setText(arg0.getAddressDetail().city); address_tv.setText(address); } @Override public void onGetGeoCodeResult(GeoCodeResult arg0) { } }); } }); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); // 定位初始化 mLocClient = new LocationClient(this); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setScanSpan(1000); mLocClient.setLocOption(option); mLocClient.start(); //点击定位按钮重新定位 dw_bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub mBaiduMap.setMyLocationEnabled(true); mLocClient.start(); } }); } /** * 定位SDK监听函数 */ public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // TODO Auto-generated method s 4000 tub // map view 销毁后不在处理新接收的位置 if (location == null || mMapView == null) { return; } // 获取经纬度 mCurrentLat = location.getLatitude(); mCurrentLon = location.getLongitude(); // 先清除图层 mBaiduMap.clear(); // 定义Maker坐标点 LatLng point = new LatLng(mCurrentLat, mCurrentLon); // 构建MarkerOption,用于在地图上添加Marker MarkerOptions options = new MarkerOptions().position(point).icon( bitmap); // 在地图上添加Marker,并显示 mBaiduMap.addOverlay(options); LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); MapStatus.Builder builder = new MapStatus.Builder(); builder.target(ll).zoom(18.0f); mBaiduMap.animateMapStatus(MapStatusUpdateFactory .newMapStatus(builder.build())); // 实例化一个地理编码查询对象 GeoCoder geoCoder = GeoCoder.newInstance(); // 设置反地理编码位置坐标 ReverseGeoCodeOption op = new ReverseGeoCodeOption(); op.location(point); // 发起反地理编码请求(经纬度->地址信息) geoCoder.reverseGeoCode(op); geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() { @Override public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) { // 获取点击的坐标地址 address = arg0.getAddress(); city_tv.setText(arg0.getAddressDetail().city); address_tv.setText(address); // 定位成功后就停止 mBaiduMap.setMyLocationEnabled(false); if (mLocClient.isStarted()) { mLocClient.stop(); }; } @Override public void onGetGeoCodeResult(GeoCodeResult arg0) { } }); } } @Override protected void onDestroy() { super.onDestroy(); mLocClient.stop(); // 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 mMapView.onDestroy(); } @Override protected void onResume() { super.onResume(); // 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); // 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理 mMapView.onPause(); } }
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="你的百度key" />
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>
上面的service一定要记得写,不然无法实现返回自己位置的功能
最终效果
相关文章推荐
- Android简单实现 高德地图的定位与显示,点击按钮切换地图图层
- 【百度地图API·javascriptapi】地图定位、创建自定义图标、获取用户点击位置
- Android移动开发-通过自定义算法代码来纠偏地图由GPS定位到的经纬度在地图上显示或解析位置时出现偏移的问题
- 百度地图定位 当前位置和地图显示
- Android简单实现 高德地图的定位与显示,点击按钮切换地图图层
- 高德地图定位按钮位置显示
- android,使用高德地图SDk(地图SDK,定位SDK,搜索SDK),显示当前位置以及自定义多个Markers
- 百度地图显示指定位置,点击显示自定义内容
- Android中使用百度API定位,并实现手势操作(显示最后点击地图的位置)
- 高德地图自定义定位当前位置按钮
- Android 百度地图(二)——地图切换、定位、添加覆盖物、搜索位置、连线
- 百度地图__定位显示在地图上
- symbian下利用基站实现定位,显示地图中自己的位置
- IOS 百度地图定位,显示地理位置
- HTML5 调用 GPSAPI 获取当前位置的坐标 显示在地图上(有包括百度地图与谷歌地图)
- html5定位获取当前位置并在百度地图上显示【转】
- android百度定位后显示在地图上点击弹出气泡
- 高德地图定位及显示位置
- android版百度地图 自定义 点击当前位置弹出气泡
- html5定位获取当前位置并在百度地图上显示