当前位置: 首页 > >

(Android)调用百度地图api之添加覆盖物

发布时间:

感谢 hyman 的视频教学课程:https://www.imooc.com/video/5686


?


一、自定义类Info

public class Info implements Serializable
{
private double latitude;
private double longitude;
private int imgId;
private String name;
public static List infos = new ArrayList();

//添加两个静态地图覆盖物
static
{
infos.add(new Info(32.120072, 118.922755, R.drawable.bookbox, "box1202"));
infos.add(new Info(32.120267, 118.920518, R.drawable.bookbox, "box1201"));
}

public Info(double latitude, double longitude, int imgId, String name)
{
this.latitude = latitude;
this.longitude = longitude;
this.imgId = imgId; //点击覆盖物,显示图片
this.name = name; //点击覆盖物,显示文字
}

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;
}
}


二、核心代码?
声明所需变量

// 覆盖物相关
private BitmapDescriptor mMarker;
public RelativeLayout mMarkerLy;


核心代码?

private void initMarker() //初始化
{
mMarker = BitmapDescriptorFactory.fromResource(R.drawable.bookbox); //设置地图覆盖物图标
mMarkerLy = findViewById(R.id.id_maker_ly); //布局文件中所对应的RelativeLayout
}



// 添加覆盖物
private void addOverlays(List infos)
{
mBaiduMap.clear();
LatLng latLng = null;
Marker marker = null;
OverlayOptions options;
for (Info info : infos)
{
// 经纬度
latLng = new LatLng(info.getLatitude(), info.getLongitude());
// 图标
options = new MarkerOptions().position(latLng).icon(mMarker)
.zIndex(5);
marker = (Marker) mBaiduMap.addOverlay(options);
Bundle arg0 = new Bundle();
arg0.putSerializable("info", info);
marker.setExtraInfo(arg0);
}
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.setMapStatus(msu);
}



mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() //设置监听
{
@Override
public boolean onMarkerClick(Marker marker)
{

Bundle extraInfo = marker.getExtraInfo();
Info info = (Info) extraInfo.getSerializable("info");
TextView name = mMarkerLy.findViewById(R.id.id_info_name);
boxname=info.getName(); //从info类中获取数据
/*connect();*/
name.setText(info.getName());
mMarkerLy.setVisibility(View.VISIBLE); //布局文件中的RelativeLayout设置为可见
return true;
}
});

mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener()
{
@Override
public boolean onMapPoiClick(MapPoi arg0)
{
return false;
}

@Override
public void onMapClick(LatLng arg0)
{
mMarkerLy.setVisibility(View.GONE);
mBaiduMap.hideInfoWindow();
/*mConnect.disconnect();*/
/*adapter=null;*/
}
});?


?三、其它

android:id="@+id/id_maker_ly"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_alignParentBottom="true"
android:background="#cc4e5a6b"
android:clickable="true"
android:visibility="gone">

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:id="@+id/id_info_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="书箱1201"
android:textColor="#fff5eb" />
android:id="@+id/recycler_view6"
android:layout_width="match_parent"
android:layout_height="match_parent">






友情链接: 时尚网 总结汇报 幼儿教育 小学教育 初中学习资料网