我正在尝试使用自定义 Overlay 类为 OSMdroid 地图制作自定义标记。我想我明白了,但它在绘图构造器上给了我一个 StackOverflowError
这是主要活动的代码
package com.example.osmap1;import java.util.List;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Overlay;
import com.example.osmap1.herramientas.MiOverlay;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
MapView map;
MapController mc;
GeoPoint gp;
List<Overlay> mOverlays;
private static MainActivity mainActivity=null;
public static MainActivity getInstance(){
if(mainActivity == null){
mainActivity=new MainActivity();
}
return mainActivity;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainActivity=this;
map=(MapView)findViewById(R.id.mapView);
mc=map.getController();
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
addPointToMapOverlay("43.528023","-5.672949");
}
private void addPointToMap (String latitud, String longitud){
String coordenadas[]={latitud,longitud};
double lat=Double.parseDouble(coordenadas[0]);
double lng=Double.parseDouble(coordenadas[1]);
GeoPoint gp=new GeoPoint((int)lat*1E6,(int)lng*1E6);
mc.animateTo(gp);
mc.setZoom(4);
/*map.invalidate(); no funciona en osm*/
}
private void addPointToMapOverlay (String latitud, String longitud){
String coordenadas[]={latitud,longitud};
double lat=Double.parseDouble(coordenadas[0]);
double lng=Double.parseDouble(coordenadas[1]);
GeoPoint gp=new GeoPoint((int)lat*1E6,(int)lng*1E6);
mOverlays=map.getOverlays();
MiOverlay marker= new MiOverlay(gp);
mOverlays.add(marker);
mc.animateTo(gp);
mc.setZoom(4);
map.invalidate();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这是我的自定义类代码
public class MiOverlay extends Overlay{
private GeoPoint punto;
public MiOverlay(GeoPoint point) {
super(MainActivity.getInstance().getBaseContext());
this.punto=point;
// TODO Auto-generated constructor stub
}
@Override
public void draw(Canvas canvas, MapView mapview, boolean shadow) {
// TODO Auto-generated method stub
this.draw(canvas, mapview, shadow);
//se traduce el punto geo localizado a un punto en la pantalla
Point scrnPoint = new Point();
mapview.getProjection().toPixels(this.punto, scrnPoint);
//se construye un bitmap a partir de la imagen
Bitmap marker = BitmapFactory.decodeResource(MainActivity.getInstance().getResources(),com.example.osmap1.R.drawable.seas);
//se dibuja la imagen del marker
canvas.drawBitmap(marker, scrnPoint.x - marker.getWidth() / 2, scrnPoint.y - marker.getHeight() / 2, null);
}
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = (GeoPoint) mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(MainActivity.getInstance().getBaseContext(),
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
}
return false;
}
}
我认为问题可能出在 mapview.getProjection().toMapPixels(this.punto, scrnPoint) 但我不确定......任何建议都很好