關於百度地圖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;
}
}