Android地图开发之OpenStreetMap基础教程
【注意】由于OpenStreetMap此类教程在国内不太多,本⽂是本⼈花费多⽇通过⼀些国外⽹站论坛总结所得,如要转载,请保留⽂章出处,尊重⼀下作者,谢谢。
开端
本⽂皆在指引⼤家在⾃⼰的项⽬⾥嵌⼊OpenStreetMap,并可以在地图上添加覆盖物、折线等常⽤地图功能。
1.0 OSM地图简介
the project that creates and provides free geographic data and mapping to anyone who wants it. We started it because most maps you think of as free actually have legal or technic 源的 地图,使⽤起来和baidu、⾼德、google等地图⼏乎⼀样的效果。
1.1 API和⽂档
相关⽹站:
1.2osmroid-android-3.0.10.jar
从openstreetmap android版官⽅⽹站下载jar包:osmroid-android-3.0.10.jar和slf4j-api-1.7.2.jar包,前者是地图库后者是个输出⽇志库,因为地图类库⾥使⽤到了,所以
两者缺⼀不可。
1.3地图实例化
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="schemas.android/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<org.osmdroid.views.MapView
android:id="@+id/myOSMmapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tilesource="MapquestOSM"
android:clickable="true"
android:enabled="true" >
</org.osmdroid.views.MapView>
</RelativeLayout>
osm.java:
⽰例代码:
mMapView = (MapView) findViewById(OSMmapview);
mController = Controller();
//ResourceProxy init
mResourceProxy = new DefaultResourceProxyImpl(this);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.setBuiltInZoomControls(true);
mMapView.setMultiTouchControls(true);
//定位当前位置,北京市西长安街复兴路
GeoPoint center = new GeoPoint(39.901873, 116.326655);
mController.setCenter(center);
mController.setZoom(14);
效果图:
初始化后,地图可以正常显⽰,双指可以缩放地图⽐例尺,但是就是不能上下左右平移地图,也就是说只能在这个区域缩放,⽆法移动,下载了官⽅链接⾥的openStreetMapViewer.apk,地图确实可以正常移动使⽤,⾄今⽆法确定具体原因,没有到能移动的动作事件监听接⼝。
1.4 ItemizedOverlayWithFocus(overlay)
添加⼀个ItemizedOerlayWithFoucs类型的overlay
⽰例代码:
//添加⾃定义image Overlay
Drawable drawable = Resources().getDrawable(
R.drawable.marker_gpsvalid);
ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
OverlayItem item = new OverlayItem("~title~", "I`m a marker,a subtitile", center);
item.setMarker(drawable);
items.add(item);
this.mLocationOverlay = new ItemizedOverlayWithFocus<OverlayItem>(
items,
google官方下载appnew ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
@Override
public boolean onItemSingleTapUp(final int index,
final OverlayItem item) {
return true;
}
@Override
public boolean onItemLongPress(final int index,
final OverlayItem item) {
return false;
}
}, mResourceProxy);
this.mLocationOverlay.setFocusItemsOnTap(true);
this.mLocationOverlay.setFocusedItem(0);
效果图:
可以添加⾃定义的Overlay,设置image。但是如代码中所⽰,实现了OnItemGestureListener接⼝,并返回true,但是单击(SingleTap)或长按(LongPress)makrer始终⽆法触发回调函数,这样就没办法显⽰⾃定义的吹出框了。官⽅的.apk可以触发此事件,实现⽅式和⽰例代码⼀样,⽬前还未确定是哪⾥的问题。
1.5 ⾃定义Overlay
⽰例代码:
//⾃定义marker图层
ArrayList<OverlayItem> markers = new ArrayList<OverlayItem>();
OverlayItem one = new OverlayItem("~one~", "custom marker", gp1);
one.setMarker(drawable);
markers.add(one);
CustomMarker marker = new CustomMarker(markers, gestureListener, mResourceProxy);
mController.setCenter(gp1);
mMapView.invalidate();
⾃定义Overlay:
/**
* ⾃定义Marker
* @author Administrator
*
*/
public class CustomMarker  extends ItemizedOverlayWithFocus<OverlayItem> implements org.osmdroid.views.overlay.ItemizedIconOverlay.OnItemGestureListener<OverlayItem>{  public CustomMarker(
List<OverlayItem> aList,
org.osmdroid.views.overlay.ItemizedIconOverlay.OnItemGestureListener<OverlayItem> aOnItemTapListener,
ResourceProxy pResourceProxy) {
super(aList, aOnItemTapListener, pResourceProxy);
}
@Override
public void addItem(int location, OverlayItem item) {
super.addItem(location, item);
}
@Override
protected boolean onTap(int index) {
Tap(index);
}
@Override
public boolean onSingleTapUp(MotionEvent event, MapView mapView) {
SingleTapUp(event, mapView);
}
@Override
public int size() {
return super.size();
}
@Override
public boolean onItemLongPress(int arg0, OverlayItem arg1) {
return true;
}
@Override
public boolean onItemSingleTapUp(int arg0, OverlayItem arg1) {
return true;
}
}
效果图:
1.6 MinimapOverlay(⼩地图)
⽰例代码:
//右下⾓⼩地图Overlay
MinimapOverlayoverlay = new MinimapOverlay(this,
效果图:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。