MapView mMapView ;
private BaiduMap mBaiduMap;
boolean isFirstLoc = true; // 是否首次定位
// 定位相关
LocationClient mLocClient;
public MyLocationListenner myListener = new MyLocationListenner();
private Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bai_du);
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
bt = (Button) findViewById(R.id.kk);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
////定义Maker坐标点
// LatLng point = new LatLng(39.963175, 116.400244);
// //构建Marker图标
// BitmapDescriptor bitmap = BitmapDescriptorFactory
// .fromResource(R.drawable.icon_hidden);
// //构建MarkerOption,用于在地图上添加Marker
// OverlayOptions option1 = new MarkerOptions()
// .position(point)
// .icon(bitmap);
// //在地图上添加Marker,并显示
// mBaiduMap.addOverlay(option1);
//定位
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 定位初始化
mLocClient = new LocationClient(this);
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setIsNeedAddress(true);//设置获得地址位置
option.setIsNeedLocationPoiList(true);//设置获得poi
option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
mLocClient.setLocOption(option);
mLocClient.start();
}
/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
String str = "定位的信息:"+location.getAddress().address+",纬度:"
+location.getLatitude();
List l = location.getPoiList();
str += ",poi数量:"+l.size()+",第一个:"+((Poi)(l.get(0))).getName();
Log.i("TAG", str);
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null) {
return;
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(5).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
String city = location.getCity();//得到城市
String district = location.getDistrict();//得到区域
String street = location.getStreet();//得到解答
String streetNumber = location.getStreetNumber();//门牌号
Log.e("------",city+district+street+streetNumber);
Intent intent=new Intent();
intent.putExtra("a",city);
intent.putExtra("b",district);
intent.putExtra("c",street);
setResult(99,intent);
if (isFirstLoc) {
isFirstLoc = false;
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()));
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//在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();
}
}
private BaiduMap mBaiduMap;
boolean isFirstLoc = true; // 是否首次定位
// 定位相关
LocationClient mLocClient;
public MyLocationListenner myListener = new MyLocationListenner();
private Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bai_du);
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
bt = (Button) findViewById(R.id.kk);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
////定义Maker坐标点
// LatLng point = new LatLng(39.963175, 116.400244);
// //构建Marker图标
// BitmapDescriptor bitmap = BitmapDescriptorFactory
// .fromResource(R.drawable.icon_hidden);
// //构建MarkerOption,用于在地图上添加Marker
// OverlayOptions option1 = new MarkerOptions()
// .position(point)
// .icon(bitmap);
// //在地图上添加Marker,并显示
// mBaiduMap.addOverlay(option1);
//定位
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 定位初始化
mLocClient = new LocationClient(this);
mLocClient.registerLocationListener(myListener);
LocationClientOption option = new LocationClientOption();
option.setIsNeedAddress(true);//设置获得地址位置
option.setIsNeedLocationPoiList(true);//设置获得poi
option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
mLocClient.setLocOption(option);
mLocClient.start();
}
/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
String str = "定位的信息:"+location.getAddress().address+",纬度:"
+location.getLatitude();
List l = location.getPoiList();
str += ",poi数量:"+l.size()+",第一个:"+((Poi)(l.get(0))).getName();
Log.i("TAG", str);
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null) {
return;
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(5).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
String city = location.getCity();//得到城市
String district = location.getDistrict();//得到区域
String street = location.getStreet();//得到解答
String streetNumber = location.getStreetNumber();//门牌号
Log.e("------",city+district+street+streetNumber);
Intent intent=new Intent();
intent.putExtra("a",city);
intent.putExtra("b",district);
intent.putExtra("c",street);
setResult(99,intent);
if (isFirstLoc) {
isFirstLoc = false;
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()));
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//在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();
}
}