百度定位开发,获取经纬度并自定义定位点图标
package com.example.testbaidu;
import android.Manifest;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.baidu.location.BDAbstractLocationListener;
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.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;
public class MainActivity extends AppCompatActivity {
private BaiduMap baiduMap;
// 定位相关
LocationClient mLocClient; //---------------first声明
MapView mMapView;
BaiduMap mBaiduMap;
private MyLocationConfiguration.LocationMode mCurrentMode;
public MyLocationListenner myListener = new MyLocationListenner();
MyLocationData locData ;
BitmapDescriptor mCurrentMarker;
boolean isFirstLoc = true;// 是否首次定位
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
// MapView mapView=findViewById(R.id.map);
// baiduMap = mapView.getMap();
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
// MyLocation location = new MyLocation();
// LocationClient client = new LocationClient(this);
// client.registerLocationListener(location);
// LocationClientOption option = new LocationClientOption();
// option.setOpenGps(true);
// option.setCoorType("bd0911");
// option.setScanSpan(1000);
// client.setLocOption(option);
// client.start();
// }
//
// class MyLocation extends BDAbstractLocationListener {
// @Override
// public void onReceiveLocation(BDLocation bdLocation) {
// double latitude = bdLocation.getLatitude();
// double longitude = bdLocation.getLongitude();
// Log.i("hh", longitude + " " + latitude);
// LatLng point=new LatLng(latitude,longitude);
// // MyLocationConfiguration.LocationMode mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;
// // BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_map_gcoding);
// BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.hza);
// OverlayOptions o1=new TextOptions().text("家乡").fontSize(50).position(point);
// OverlayOptions o2=new MarkerOptions()
// .position(point)
// .icon(bitmap)//perspective透视效果设置
// .perspective(true).animateType(MarkerOptions.MarkerAnimateType.jump);
// // baiduMap.setMyLocationConfiguration(new MyLocationConfiguration(mCurrentMode, false, bitmap));
// baiduMap.addOverlay(o1);
// baiduMap.addOverlay(o2);
// //MapStatus mMapStatus = new MapStatus.Builder().target(point).zoom(20).build();//zoom设置地图缩放级别
// /**
// * 将定位到的地方移到地图中央
// */
// MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(point);
// baiduMap.setMapStatus(msu);// 设置地图放大缩小参数
// baiduMap.animateMapStatus(msu);
// }
// }
mCurrentMode = MyLocationConfiguration.LocationMode.FOLLOWING;
// 1 地图初始化
mMapView = findViewById(R.id.map);//经度120.219375,纬度30.259244 杭州
//mMapView = new MapView(this);
//mMapView.refreshDrawableState();
System.out.println("初始化成功啦,哈哈!");
mBaiduMap = mMapView.getMap();
// 2 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker));
// 3 定位初始化
mLocClient = new LocationClient(this);
mLocClient.registerLocationListener(myListener); //---------------first注册定位监听
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gps
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(3000);
option.setIsNeedAddress(true);
mLocClient.setLocOption(option); //---------------first设置locationoption
mLocClient.start(); //---------------first启动sdk定位
mLocClient.requestLocation();
}
/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null)
return;
locData= new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(location.getDirection()).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
System.out.println("location.lat"+location.getLatitude()+"&&&&&&&&"+"location.long"+location.getLongitude());
//构建Marker图标
BitmapDescriptor bitmap = BitmapDescriptorFactory
.fromResource(R.mipmap.hza);//此处设置自己的图标即可
//构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions()
.position(new LatLng(location.getLatitude(),location.getLongitude()))
.icon(bitmap);
//在地图上添加Marker,并显示
mBaiduMap.addOverlay(option);
mBaiduMap.setMyLocationEnabled(true); //------1允许定位图层
mBaiduMap.setMyLocationData(locData); //------2允许定位图层后在设置定位数据
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll,19);
mBaiduMap.animateMapStatus(u);
if (isFirstLoc) {
isFirstLoc = false;
// 设置地图缩放比例:17级100米
MapStatusUpdate ms = MapStatusUpdateFactory.newLatLngZoom(ll,19);
System.out.println("33333333333333333");
mBaiduMap.setMapStatus(ms);
}
}
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
mLocClient.unRegisterLocationListener(myListener);
// 退出时销毁定位
mLocClient.stop();
// 关闭定位图层
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mMapView = null;
super.onDestroy();
}
}