我正在使用 Open Street View,当用户触摸地图上的特定位置时,我需要弹出一个窗口。参考了一些教程后,我发现我必须编写一个名为 BalloonOverLay 的类。贝娄是我的代码:
公共类 AndroidOpenStreetMapViewActivity 扩展 Activity {
private MapView myOpenMapView;
private MapController myMapController;
LocationManager locationManager;
ArrayList<OverlayItem> overlayItemArray;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myOpenMapView = (MapView)findViewById(R.id.openmapview);
myOpenMapView.setBuiltInZoomControls(true);
myMapController = myOpenMapView.getController();
myMapController.setZoom(12);
//--- Create Overlay
overlayItemArray = new ArrayList<OverlayItem>();
DefaultResourceProxyImpl defaultResourceProxyImpl
= new DefaultResourceProxyImpl(this);
MyItemizedIconOverlay myItemizedIconOverlay
= new MyItemizedIconOverlay(
overlayItemArray, null, defaultResourceProxyImpl);
myOpenMapView.getOverlays().add(myItemizedIconOverlay);
//---
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//for demo, getLastKnownLocation from GPS only, not from NETWORK
Location lastLocation
= locationManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER);
if(lastLocation != null){
updateLoc(lastLocation);
}
//Add Scale Bar
ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this);
myOpenMapView.getOverlays().add(myScaleBarOverlay);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, myLocationListener);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
locationManager.removeUpdates(myLocationListener);
}
private void updateLoc(Location loc){
GeoPoint locGeoPoint = new GeoPoint(loc.getLatitude(), loc.getLongitude());
myMapController.setCenter(locGeoPoint);
setOverlayLoc(loc);
myOpenMapView.invalidate();
}
private void setOverlayLoc(Location overlayloc){
GeoPoint overlocGeoPoint = new GeoPoint(overlayloc);
//---
overlayItemArray.clear();
OverlayItem newMyLocationItem = new OverlayItem(
"My Location", "My Location", overlocGeoPoint);
overlayItemArray.add(newMyLocationItem);
//---
}
private LocationListener myLocationListener
= new LocationListener(){
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
updateLoc(location);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
私有类 MyItemizedIconOverlay 扩展 ItemizedIconOverlay{
public MyItemizedIconOverlay(
List<OverlayItem> pList,
org.osmdroid.views.overlay.ItemizedIconOverlay.OnItemGestureListener<OverlayItem> pOnItemGestureListener,
ResourceProxy pResourceProxy) {
super(pList, pOnItemGestureListener, pResourceProxy);
// TODO Auto-generated constructor stub
}
@Override
public void draw(Canvas canvas, MapView mapview, boolean arg2) {
// TODO Auto-generated method stub
super.draw(canvas, mapview, arg2);
if(!overlayItemArray.isEmpty()){
//overlayItemArray have only ONE element only, so I hard code to get(0)
GeoPoint in = overlayItemArray.get(0).getPoint();
Point out = new Point();
mapview.getProjection().toPixels(in, out);
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_menu_mylocation);
canvas.drawBitmap(bm,
out.x - bm.getWidth()/2, //shift the bitmap center
out.y - bm.getHeight()/2, //shift the bitmap center
null);
}
}
@Override
public boolean onSingleTapUp(MotionEvent event, MapView mapView) {
// TODO Auto-generated method stub
//return super.onSingleTapUp(event, mapView);
return true;
}
}
我参考了以下教程:
http://android-coding.blogspot.com/2012/06/example-of-implementing-openstreetmap.html#comment-form http://code.google.com/p/osmdroid/ http://android-er .blogspot.in/2012/05/create-multi-marker-openstreetmap-for.html