关于百度地图InfoWindow响应自定义布局点击事件
关于百度地图InfoWindow响应自定义布局点击事件
大概讲解:
在百度地图上显示一个marker,当marker被点击后,会弹出一个自定义view,当时在公司做这个功能,被坑惨了,死活弹不出来,不响应.接下来看一下效果图,代码有详细注释,进去之后把百度申请的秘钥换成自己的.有一部分是检测网络代码.这个不用管.
代码如下:
public class MainActivity extends Activity implements OnMapClickListener,OnMarkerClickListener {
private IntentFilter filter;
private MapView mMapView = null;
private NetworkChangeReceiver changeReceiver;
private LatLng ll, ll2, position2;
private BitmapDescriptor bitmap;
private MarkerOptions markerOptions;
private Marker marker;
private BaiduMap map;
private InfoWindow mInfoWindow;
private ViewGroup infoView;
public static int flag1 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 初始化地图
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
init();
// 查询网络代码
filter = new IntentFilter();
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
changeReceiver = new NetworkChangeReceiver();
registerReceiver(changeReceiver, filter);
}
private void init() {
// TODO Auto-generated method stub
map = mMapView.getMap();
mMapView = (MapView) findViewById(R.id.bmapView);
mMapView.showZoomControls(false);// 去除缩放功能
mMapView.showScaleControl(false);// 去除标尺
mMapView.removeViewAt(1);// 去除百度LOGO
// 定位中心位置
ll = new LatLng(32.0647517242, 118.8029140176);//坐标
MapStatus mMapStatus = new MapStatus.Builder().target(ll).zoom(13)
.build();
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory
.newMapStatus(mMapStatus);
map.setTrafficEnabled(true);
map.setMapStatus(mMapStatusUpdate);
map.setOnMapClickListener(this);//设置 地图点击事件
map.setOnMarkerClickListener(this);//设置覆盖物点击事件
//添加覆盖物图标
bitmap = BitmapDescriptorFactory.fromResource(R.drawable.pic_gzyi);
LatLng ll3 = new LatLng(31.937775, 118.777021);
markerOptions = new MarkerOptions().icon(bitmap).position(ll3)
.animateType(MarkerAnimateType.grow).perspective(true);
marker = (Marker) map.addOverlay(markerOptions);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(changeReceiver);
}
class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
ConnectivityManager connectionMange = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info = connectionMange.getActiveNetworkInfo();
if (info != null && info.isAvailable()) {
Toast.makeText(context, "网络开启", 0).show();
} else {
Toast.makeText(context, "网络关闭", 0).show();
}
}
}
// 覆盖物点击事件
@Override
public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub
if (flag1 == 0)
{
Toast.makeText(MainActivity.this, "欢迎来到百度地图页面", 0).show();
position2 = arg0.getPosition();
double latitude = position2.latitude;
// ll2 = new LatLng(31.937775, 118.777021);
MapStatus mMapStatus = new MapStatus.Builder()
.target(position2).zoom(15).build();
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory
.newMapStatus(mMapStatus);
map.setTrafficEnabled(true);
map.setMapStatus(mMapStatusUpdate);
mInfoWindow = new InfoWindow((getInfoWindoView(marker)),
position2, -47);// 把弹出框的view添加到InfoWindow,关键就是这句代码
map.showInfoWindow(mInfoWindow);
BaiduMap map = mMapView.getMap();
flag1 = 1;
} else {
map.hideInfoWindow();
flag1 = 0;
}
return true;
}
//这个是覆盖物弹出框
private View getInfoWindoView(Marker marker2) {
// TODO Auto-generated method stub
if (null == infoView) {
infoView = (ViewGroup) LayoutInflater
.from(getApplication()).inflate(
R.layout.overlay_layout4, null);//这个是点击覆盖物弹出的也面,可以自己定义
}
return infoView;
}
//地图点击事件
@Override
public void onMapClick(LatLng arg0) {
// TODO Auto-generated method stub
map.hideInfoWindow();
}
@Override
public boolean onMapPoiClick(MapPoi arg0) {
// TODO Auto-generated method stub
return false;
}
}