public class MainActivity extends Activity implements View.OnLongClickListener {
private static final String TAG = MainActivity.class.getSimpleName();
// 定位相关
LocationClient mLocClient;
public MyLocationListenner myListener = new MyLocationListenner();
MapView mMapView;
BaiduMap mBaiduMap;
boolean isFirstLoc = true; // 是否首次定位
private LatLng point;
private Marker markerA;
private Marker markerB;
private Marker markerC;
private LatLng point2;
private LatLng point3;
private GeoCoder geoCoder;
private Button button1;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
// 地图初始化
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
// 创建地理编码检索实例
geoCoder = GeoCoder.newInstance();
// 定位初始化
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();
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
button = new Button(MainActivity.this);
button.setBackgroundResource(R.drawable.btn);
if (marker == markerA) {
button.setText("我是A");
myLatLng(point);
//创建InfoWindow , 传入 view, 地理坐标, y 轴偏移量
InfoWindow mInfoWindow = new InfoWindow(button, point, -110);
//显示InfoWindow
mBaiduMap.showInfoWindow(mInfoWindow);
}
if (marker == markerB) {
button.setText("我是B");
InfoWindow mInfoWindow = new InfoWindow(button, point2, -110);
mBaiduMap.showInfoWindow(mInfoWindow);
}
if (marker == markerC) {
button.setText("我是C");
InfoWindow mInfoWindow = new InfoWindow(button, point3, -110);
mBaiduMap.showInfoWindow(mInfoWindow);
}
// Toast.makeText(MainActivity.this, ""+point.latitude+":"+point.longitude+":"+marker, Toast.LENGTH_SHORT).show();
return false;
}
});
mBaiduMap.setOnMapLongClickListener(new BaiduMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng latLng) {
MapStatus maps = new MapStatus.Builder().target(latLng).zoom(18).build();
MapStatusUpdate m = MapStatusUpdateFactory.newMapStatus(maps);
//改变地图状态
mBaiduMap.setMapStatus(m);
// Log.i(TAG, "onMapLongClick: "+latLng.latitude+":"+latLng.longitude);
// I/MainActivity: onMapLongClick: 22.54988914359645:113.93814237509572
}
});
}
@Override
public boolean onLongClick(View view) {
return false;
}
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
event.getDownTime();
Log.i(TAG, "onKeyLongPress: " + event.getDownTime());
return super.onKeyLongPress(keyCode, event);
}
/**
* 定位SDK监听函数
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null) {
return;
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
latitude = location.getLatitude();
longitude = location.getLongitude();
// Log.i(TAG, "onReceiveLocation: " + location.getLongitude() + ":" + location.getLatitude());
addMyLocation();
mBaiduMap.setMyLocationData(locData);
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()));
}
}
}
//当前位置经纬度
private double latitude;
private double longitude;
/**
* 定位并添加标注
*/
private void addMyLocation() {
List<OverlayOptions> var = new ArrayList();
//定义Maker坐标点
point = new LatLng(latitude, longitude);
//构建Marker图标
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_marka);
//构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions()
.position(point)
.icon(bitmap);
var.add(option);
point2 = new LatLng(latitude + 0.001123, longitude + 0.001256);
//构建Marker图标
BitmapDescriptor bitmapb = BitmapDescriptorFactory.fromResource(R.drawable.icon_markb);
//构建MarkerOption,用于在地图上添加Marker
OverlayOptions option2 = new MarkerOptions()
.position(point2)
.icon(bitmapb);
//在地图上添加Marker,并显示
var.add(option2);
point3 = new LatLng(latitude - 0.001123, longitude - 0.001256);
//构建Marker图标
BitmapDescriptor bitmapc = BitmapDescriptorFactory.fromResource(R.drawable.icon_markc);
//构建MarkerOption,用于在地图上添加Marker
OverlayOptions option3 = new MarkerOptions()
.position(point3)
.icon(bitmapc);
//在地图上添加Marker,并显示
var.add(option3);
mBaiduMap.addOverlays(var);
markerA = (Marker) mBaiduMap.addOverlay(option);
markerB = (Marker) mBaiduMap.addOverlay(option2);
markerC = (Marker) mBaiduMap.addOverlay(option3);
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
// 退出时销毁定位
mLocClient.stop();
// 关闭定位图层
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mMapView = null;
super.onDestroy();
}
/**
* 经纬度转换成地址
*/
private void myLatLng(LatLng latLng) {
// 设置反地理经纬度坐标,请求位置时,需要一个经纬度
geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(latLng));
//设置地址或经纬度反编译后的监听,这里有两个回调方法,
geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
@Override
public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
// addre = "地址:"+reverseGeoCodeResult.getAddress();
// Log.i(TAG, "onGetReverseGeoCodeResult: "+reverseGeoCodeResult.getAddress());
}
/**
*
* @param reverseGeoCodeResult
*/
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
if (reverseGeoCodeResult == null || reverseGeoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(MainActivity.this, "找不到该地址!", Toast.LENGTH_SHORT).show();
}
Log.i(TAG, "onGetGeoCodeResult: " + reverseGeoCodeResult.getAddress());
// String addre = "地址:"+geoCodeResult.getAddress();
button.setText( "地址:"+reverseGeoCodeResult.getAddress());
// I/MainActivity: onGetReverseGeoCodeResult: 地址:广东省深圳市南山区科苑路15号
}
});
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yang.yin.myapplication.MainActivity">
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:clickable="true" />
</RelativeLayout>
相关配置
sourceSets{
main{
jniLibs.srcDir 'libs'