gpt4 book ai didi

android - 如何在 map 上画很多标记

转载 作者:行者123 更新时间:2023-11-29 02:10:09 25 4
gpt4 key购买 nike

我正在尝试使用标记在 map 上绘制。我遇到的问题是当我尝试添加很多时,我无法运行它。为了说明我的问题,假设我想绘制一个由 900 个较小的正方形 (30x30) 组成的正方形。顺便说一句,我确实需要那些方 block ,而不仅仅是一个大方 block 我正在尝试将标记添加到 itemizedoverlay,然后绘制它。

代码如下。有什么建议我该怎么做?非常感谢!!

public class MainActivity extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
static Context context;

int k=1;
List<Overlay> mapOverlays;
MyItemizedOverlay itemizedoverlay;
int[] xx,yy;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext();
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);

mc = mapView.getController();
String coordinates[] = {"36.186382", "-112.221909"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(10);

mapOverlays = mapView.getOverlays();
Drawable squareM = this.getResources().getDrawable(R.drawable.square);
itemizedoverlay = new MyItemizedOverlay(squareM);

int n=0;
xx = new int[30];
yy = new int[30];
while(n<30){
xx[n]=n;
yy[n]=n;
n++;
}

}

protected void drawNewMarker(GeoPoint p){
OverlayItem overlayitem = new OverlayItem(p, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mapView.postInvalidate();
}

protected void drawNewMarkerD(){
Point pointP = new Point(0,0);
mapView.getProjection().toPixels(p, pointP);
int xoff = 20*k;
int yoff = 20*k;
GeoPoint pp = mapView.getProjection().fromPixels(pointP.x + xoff, pointP.y + yoff);

OverlayItem overlayitem = new OverlayItem(pp, "", "");
Drawable iconM = this.getResources().getDrawable(R.drawable.icon);
iconM.setBounds(0,0, 50, 50);
overlayitem.setMarker(iconM);
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
k++;
//mapView.postInvalidate();
}

@Override
protected boolean isRouteDisplayed() {

return false;
}


protected void drawGrid(int xoff, int yoff){
GeoPoint p = newGeoPointFromOffset(xoff, yoff);
OverlayItem overlayitem = new OverlayItem(p, "", "");
Drawable marker = this.getResources().getDrawable(R.drawable.grid);
marker.setBounds(0,0, 15, 15);
overlayitem.setMarker(marker);
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}

protected GeoPoint newGeoPointFromOffset(int xoff, int yoff){
Point screenPointP = new Point(0,0);
mapView.getProjection().toPixels(p, screenPointP);
int x = 15*xoff;
int y = 15*yoff;
return mapView.getProjection().fromPixels(screenPointP.x + x, screenPointP.y + y);
}

public boolean onKeyDown(int keyCode, KeyEvent event){
MapController mc = mapView.getController();
switch(keyCode){
case KeyEvent.KEYCODE_3:
mc.zoomIn();
break;
case KeyEvent.KEYCODE_1:
mc.zoomOut();
break;
case KeyEvent.KEYCODE_0:
drawNewMarker(p);
break;
case KeyEvent.KEYCODE_9:
for(int r=0;r<30;r++){
for(int s=0;s<30;s++){
drawGrid(r,s);
}
}
mapView.postInvalidate();
break;
}
return super.onKeyDown(keyCode, event);
}

public class MyItemizedOverlay extends ItemizedOverlay{
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;

public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}

public MyItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}

public void draw(android.graphics.Canvas canvas, MapView mapView,boolean shadow) {
super.draw(canvas, mapView, false);
}

public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}

@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}

最佳答案

MapView 可以处理的叠加项数量是有限制的。没有关于此限制的官方消息,但一段时间后,您会在缩放/平移期间看到缓慢的行为。

一些解决方案是:

  • > LazyLoading of MapView
  • 只加载尽可能多的可见标记,根据缩放级别和跨度执行此操作。
  • 10 个 99 点叠加比 1 个 900 点叠加快。

关于android - 如何在 map 上画很多标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7934978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com