百度地图点击标识点Marker,弹出详情窗InfoWindow
弹出详情窗主要要用到mBaiduMap.setOnMarkerClickListener(marker_Listener);方法
在给mBaiduMap设置setOnMarkerClickListener监听器前,要定义详情窗InfoWindow ,以及给详情窗InfoWindow 设置监听器。
这里要注意的是,定义详情窗InfoWindow时,如果是调用BitmapDescriptorFactory.fromView(infoWindow_view);方法,那么在infoWindow_view的xml布局中的根元素layout下需要有其他的UI控件做支撑,否则BitmapDescriptorFactory.fromView(infoWindow_view);方法会报空指针异常
package com.example.mapdemo02;
import java.util.ArrayList;
import java.util.List;
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.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.InfoWindow.OnInfoWindowClickListener;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
MapView mMapView;//百度地图控件
BaiduMap mBaiduMap;//地图图层
TextView textView_findCourier;//UI显示其他标识点
BitmapDescriptor bitmap_otherPoint;//标识点图片
long backTime;//系统退出时间
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
showCourierPoint();//显示标志点
showInfoWindow();//显示详情窗
}
//点击显示详情窗
private void showInfoWindow() {
OnMarkerClickListener marker_Listener = new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
//在显示新信息窗之前,先关闭已经在显示的信息窗
mBaiduMap.hideInfoWindow();
//显示信息窗
View infoWindow_view = getLayoutInflater().inflate(R.layout.item_infowindow, null);
TextView textView_item_infoWindow = (TextView) infoWindow_view.findViewById(R.id.textView_item_infoWindow);
//如果显示窗是要用自定义的view,则最外层可直接用UI控件或者layout,如果外层用layout来布局,那么布局中一定要有UI控件,否则BitmapDescriptorFactory.fromView(view);会报空指针
// Bundle bundle = marker.getExtraInfo();
// textView_item_infoWindow.setText(bundle.getString("name"));
textView_item_infoWindow.setText("MAP");
BitmapDescriptor infoWindow_bitmap = BitmapDescriptorFactory.fromView(infoWindow_view);
//信息窗点击处理事件
Log.d("tag", "infoWindow_view="+infoWindow_view);
Log.d("tag", "infoWindow_bitmap="+infoWindow_bitmap);
OnInfoWindowClickListener infoWindow_ClickListener = new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick() {
// TODO Auto-generated method stub
mBaiduMap.hideInfoWindow();
}
};
//定义信息窗
InfoWindow infoWindow = new InfoWindow(infoWindow_bitmap,//信息窗布局
marker.getPosition(), //信息窗的点
100, //信息窗与点的位置关系
//infoWindow监听器
infoWindow_ClickListener
);
//显示信息窗
mBaiduMap.showInfoWindow(infoWindow);
return true;
}
};
//地图设置marker的点击事件
mBaiduMap.setOnMarkerClickListener(marker_Listener);
}
//点击获取标志点的位置并显示
private void showCourierPoint() {
bitmap_otherPoint = BitmapDescriptorFactory.fromResource(R.drawable.point_blue);
textView_findCourier = (TextView) findViewById(R.id.textView_findCourier);
OnClickListener textView_findCourier_OnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
LatLng p01 = new LatLng(22.654249, 114.07256);
LatLng p02 = new LatLng(22.614249, 114.03256);
LatLng p03 = new LatLng(22.654249, 114.02256);
OverlayOptions Overlay_p01 = new MarkerOptions().position(p01).icon(bitmap_otherPoint);
OverlayOptions Overlay_p02 = new MarkerOptions().position(p02).icon(bitmap_otherPoint);
OverlayOptions Overlay_p03 = new MarkerOptions().position(p03).icon(bitmap_otherPoint);
lists_OverlayOptions.add(Overlay_p01);
lists_OverlayOptions.add(Overlay_p02);
lists_OverlayOptions.add(Overlay_p03);
mBaiduMap.addOverlays(lists_OverlayOptions);
}
};
textView_findCourier.setOnClickListener(textView_findCourier_OnClickListener);
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
mMapView.onResume();
super.onResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
mMapView.onPause();
super.onPause();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
mMapView.onDestroy();
mMapView = null;
super.onDestroy();
}
@Override
public void onBackPressed() {
if (System.currentTimeMillis()-backTime<2000) {
finish();
}else {
backTime = System.currentTimeMillis();
Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
}
}
}
详情窗的xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="80dp" android:layout_height="30dp" android:background="#00ffaa" >
<TextView android:id="@+id/textView_item_infoWindow" android:layout_width="match_parent" android:layout_height="match_parent" android:text="XXX" />
</RelativeLayout>
mainactivity布局xml:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mapdemo02.MainActivity" >
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" />
<TextView
android:id="@+id/textView_findCourier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示标志点"
android:background="#ff00aa"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="5dp"
android:clickable="true"
/>
</RelativeLayout>