1
我正在嘗試使用自定義覆蓋類爲OSMdroid貼圖製作自定義標記。我覺得我得到它,但它使我對平局構造器osm中的自定義覆蓋標記
這裏一個的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),但我不確定...任何消化都很順利
是的,這是循環,但Overlay類的繪製構造函數不接受super.draw(canvas,mapview,shadow),而是調用super.drawAt(canvas,Drawable,x,y,shadow)。 。也許它會比調用位圖更好。我正在從使用Google地圖的另一種做法中解脫出來,這就是問題所在:P –