您的位置:首页 > 移动开发 > Android开发

Android demo-->百度地图sdk,AS版本(三)添加覆盖物,InfoWindow的使用

2017-03-03 16:50 543 查看
1创建实体类承载信息

2添加覆盖物信息

3为地图上的Maker添加点击事件

1、创建实体类,承载信息

public class Info implements Serializable {
private static final long serialVersionUID = -758459502806858414L;
/**
* 经度
*/
private double latitude;

/**
* 纬度
*/
private double longitude;

/**
* 图片id,真实项目中是图片路径
*/
private int imgId;

/**
* 商家名称
*/
private String name;

/**
* 距离
*/
private String distance;

/**
* 赞数量
*/
private int zan;

public Info(){

}

public Info(double latitude,double longitude,int imgId,String name,String distance,int zan){
super();
this.latitude=latitude;
this.longitude=longitude;
this.imgId=imgId;
this.name=name;
this.distance=distance;
this.zan=zan;
}

public static List<Info> infos=new ArrayList<Info>();

static {
infos.add(new Info(34.242652, 108.971171, R.mipmap.a01, "英伦贵族小旅馆","距离209米", 1456));
infos.add(new Info(34.242952, 108.972171, R.mipmap.a02, "沙井国际洗浴会所",
"距离897米", 456));
infos.add(new Info(34.242852, 108.973171, R.mipmap.a03, "五环服装城",
"距离249米", 1456));
infos.add(new Info(34.242152, 108.971971, R.mipmap.a04, "老米家泡馍小炒",
"距离679米", 1456));

}

public double getLatitude() {
return latitude;
}

public void setLatitude(double latitude) {
this.latitude = latitude;
}

public double getLongitude() {
return longitude;
}

public void setLongitude(double longitude) {
this.longitude = longitude;
}

public int getImgId() {
return imgId;
}

public void setImgId(int imgId) {
this.imgId = imgId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDistance() {
return distance;
}

public void setDistance(String distance) {
this.distance = distance;
}

public int getZan() {
return zan;
}

public void setZan(int zan) {
this.zan = zan;
}
}


在实体类中声明了一个静态列表集合,模拟从服务器返回的数据。

2、添加覆盖物信息

private void addInfosOverlay(List<Info> infos) {
mBaiduMap.clear();
LatLng latLng = null;
OverlayOptions overlayOptions = null;
Marker marker = null;
//遍历info传入marker
for (Info info : infos) {
//位置
latLng = new LatLng(info.getLatitude(), info.getLongitude());
//图标
overlayOptions = new MarkerOptions().position(latLng).icon(mIconMaker).zIndex(5);
marker = (Marker) mBaiduMap.addOverlay(overlayOptions);
Bundle bundle = new Bundle();
bundle.putSerializable("info", info);
marker.setExtraInfo(bundle);
}
//将地图移动到最后一个经纬度位置
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.setMapStatus(u);
}


3、为地图上的Maker添加点击事件

mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
//获得marker中的数据
Info info = (Info) marker.getExtraInfo().get("info");
InfoWindow infoWindow;
//生成一个TextView用户在地图中显示InfoWindow
TextView location = new TextView(getApplicationContext());
location.setBackgroundResource(R.mipmap.location_tips);
location.setPadding(30, 20, 30, 50);
location.setText(info.getName());
//将marker所在的经纬度的信息转化成屏幕上的坐标
final LatLng ll = marker.getPosition();
android.graphics.Point p = mBaiduMap.getProjection().toScreenLocation(ll);
Log.e(TAG, "--" + p.x + " , " + p.y);
p.y -= 47;
LatLng llInfo = mBaiduMap.getProjection().fromScreenLocation(p);

//为弹出的infoWindow添加点击事件
infoWindow = new InfoWindow(mIconMaker, llInfo,0, new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick() {
//隐藏InfoWindow
mBaiduMap.hideInfoWindow();
}
});
mBaiduMap.showInfoWindow(infoWindow);
//设置详细信息布局为可见
id_marker_info.setVisibility(View.VISIBLE);
//根据商家信息为详细信息布局设置信息
popupInfo(id_marker_info,info);

return true;
}
});

/**
* 根据info为布局上的控件设置信息
*/
protected void popupInfo(RelativeLayout id_marker_info,Info info){
ViewHolder viewHolder=null;
if (id_marker_info.getTag()==null){
viewHolder=new ViewHolder();
viewHolder.infoImg= (ImageView) id_marker_info.findViewById(R.id.info_img);
viewHolder.infoName= (TextView) id_marker_info.findViewById(R.id.info_name);
viewHolder.infoDistance= (TextView) id_marker_info.findViewById(R.id.info_distance);
viewHolder.infoZan= (TextView) id_marker_info.findViewById(R.id.info_zan);
id_marker_info.setTag(viewHolder);
}
viewHolder= (ViewHolder) id_marker_info.getTag();
viewHolder.infoImg.setImageResource(info.getImgId());
viewHolder.infoName.setText(info.getName());
viewHolder.infoDistance.setText(info.getDistance());
viewHolder.infoZan.setText(info.getZan()+"");
}

/**
* 复用弹出面板mMarkerLy的控件
*/
private class ViewHolder{
ImageView infoImg;
TextView infoName;
TextView infoDistance;
TextView infoZan;
}


最后添加地图的点击事件,隐藏出现的详细信息布局和InfoWindow。

mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
id_marker_info.setVisibility(View.GONE);
mBaiduMap.hideInfoWindow();
}

@Override
public boolean onMapPoiClick(MapPoi mapPoi) {
return false;
}
});


项目名称:BaiDuMap
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐