因为项目的原因,对百度API接口更加的深入了了解。废话不多说,上效果图
界面代码
<RelativeLayout
android:id="@+id/map_parent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3" >
<com.baidu.mapapi.map.MapView
android:id="@+id/id_bmapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" />
<ImageView
android:id="@+id/img"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="@drawable/icon_start_location" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5" >
<ListView
android:id="@+id/lv_select_road"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
没有什么难点,要注意的地方是,MapView中间存在有一张ImagView。这张图片不会随着MapView的滑动而变化位置。但是下面的ListView就会随着MapView的滑动进行改变。(其实滑动的过程中就是经纬度发生了变化,而MapView就是一个定位点)。
好吧,好绕。还是看代码吧。
// 地图的状态发生改变的时候
baidumap.setOnMapStatusChangeListener(new MapRemoveListener() {
@Override
public void onMapStatusChangeStart(MapStatus arg0) {
String url = String.format(HttpConstant.GET_NEAR_POITS,
arg0.target.latitude + "," + arg0.target.longitude);
controller.getNearPois(url, SelectRoadActivity.this);
}
});
在你拖动BaiduMap的时候它会触发这个函数。然后在这里获取经纬度信息,拼凑一个String出来。
public static final String GET_NEAR_POITS = "http://api.map.baidu.com/geocoder/v2/?ak="
+ HttpConstant.APPDATA.BAIDUMAP_KEY
+ "&location=%s&output=json&pois=1";
HttpConstant.APPDATA.BAIDUMAP_KEY:是百度server端的AK;(自己申请)
这里要注意的是,获取经纬度之后,我们把经纬度拼凑成url然后改用get方式去获取。该经纬度附近的点。然后赋值给ListView。