使用osmbonusPack查询附近的POI并标记实现代码如下:
package com.osmbonus.simple; import java.util.ArrayList; import org.osmdroid.bonuspack.location.NominatimPOIProvider; import org.osmdroid.bonuspack.location.POI; import org.osmdroid.bonuspack.overlays.ExtendedOverlayItem; import org.osmdroid.bonuspack.overlays.ItemizedOverlayWithBubble; import org.osmdroid.tileprovider.tilesource.TileSourceFactory; import org.osmdroid.util.GeoPoint; import org.osmdroid.views.MapController; import org.osmdroid.views.MapView; import org.osmdroid.views.overlay.OverlayItem; import android.app.Activity; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; /** * * 查询相关的POI并标记marker和气泡 * * 备注:需要的注意事项: * 1.针对OSMBonusPack v2.x jars 是基于 osmdroid v3.0.8. 所以版本必须对应否则可能出现莫名其妙的问题。 * 2.以下是集成必须的资源信息 * •For a white bubble: * ◦drawable-mpi/bonuspack_bubble.9.png * ◦layout/bonuspack_bubble.xml * •For a dark-grey bubble: * ◦drawable-mpi/bonuspack_bubble_black.9.png * ◦layout/bonuspack_bubble_black.xml * •For the "more info" button: * ◦drawable/btn_moreinfo.xml * ◦drawable-mpi/moreinfo_arrow.png * ◦drawable-mpi/moreinfo_arrow_pressed.png * 3.地图使用的权限总结: * <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> * <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> * <uses-permission android:name="android.permission.INTERNET" /> * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> * *针对OSMBonusPack中几个重要的特性: * 1.根据地点名称查询POI * 2.查询附近的POI * 3.根据起始点规划线路 * 4.marker气泡重写和定制 * 5.地图poi点的响应处理 * * * * @author longgangbai * * 以下代码均来自官方Tutorial_1 整理 * */ public class OSMBonusPackSimpleActivity extends Activity { private MapView map; private ItemizedOverlayWithBubble<ExtendedOverlayItem> poiMarkers; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取地图对象 map = (MapView) findViewById(R.id.map); //设置地图图源 map.setTileSource(TileSourceFactory.MAPNIK); //设置起始点和中心 GeoPoint startPoint = new GeoPoint(48.13, -1.63); MapController mapController = map.getController(); mapController.setCenter(startPoint); mapController.setZoom(9); final ArrayList<ExtendedOverlayItem> poiItems = new ArrayList<ExtendedOverlayItem>(); poiMarkers = new ItemizedOverlayWithBubble<ExtendedOverlayItem>(this,poiItems, map); NominatimPOIProvider poiProvider = new NominatimPOIProvider(); //查询服务的该点的附近的atm ArrayList<POI> pois = poiProvider.getPOICloseTo(startPoint, "atm", 50, 0.1); //ArrayList<POI> pois = poiProvider.getPOIAlong(road.getRouteLow(), "fuel", 50, 2.0); //Wikipedia POIs with GeoNames 需要申请Account //GeoNamesPOIProvider poiProvider = new GeoNamesPOIProvider("YOUR_GEONAMES_ACCOUNT"); //BoundingBoxE6 bb = map.getBoundingBox(); //ArrayList<POI> pois = poiProvider.getPOIInside(bb, 75); //采用 Flickr 需要申请API KEY //FlickrPOIProvider poiProvider = new FlickrPOIProvider("YOUR_FLICKR_API_KEY"); //BoundingBoxE6 bb = map.getBoundingBox(); //ArrayList<POI> pois = poiProvider.getPOIInside(bb, 20); if(pois!=null){ for (POI poi : pois){ //设置气泡的信息 ExtendedOverlayItem poiMarker = new ExtendedOverlayItem(poi.mType, poi.mDescription,poi.mLocation, map.getContext()); //设置气泡的默认图标 Drawable marker = getResources().getDrawable(R.drawable.marker_poi_default); poiMarker.setMarker(marker); //设置气泡的位置 poiMarker.setMarkerHotspot(OverlayItem.HotspotPlace.CENTER); //设置POI对应的图标信息 if (poi.mThumbnail != null){ poiMarker.setImage(new BitmapDrawable(poi.mThumbnail)); } //添加一个marker点 poiMarkers.addItem(poiMarker); } } map.getOverlays().add(poiMarkers); //刷新地图 map.invalidate(); } }