21

我想做以下事情,并且在这些事情上停留了几天:

  1. 我试图绘制当我移动地图时移动的折线(我已经编码了折线,但已经设法解码了这些折线)。
    我找到的唯一解决方案是将地理点转换为屏幕坐标……如果我移动地图,它就不会移动。

  2. 我曾经HelloItemizedOverlay添加大约150 个标记,它变得非常非常慢
    知道该怎么做吗?我在考虑线程(处理程序)。

  3. 我一直在寻找某种定时器函数,它定期执行给定的函数,比如每 1 分钟左右。

  4. 我还在寻找从所有标记/线条等中清除 Google 地图的方法。

4

5 回答 5

11

答案如下:

1)这是我使用的解决方案:

/** Called when the activity is first created. */
private List<Overlay> mapOverlays;

private Projection projection;  

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    linearLayout = (LinearLayout) findViewById(R.id.zoomview);
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    mapOverlays = mapView.getOverlays();        
    projection = mapView.getProjection();
    mapOverlays.add(new MyOverlay());        

}

@Override
protected boolean isRouteDisplayed() {
    return false;
}

class MyOverlay extends Overlay{

    public MyOverlay(){

    }   

    public void draw(Canvas canvas, MapView mapv, boolean shadow){
        super.draw(canvas, mapv, shadow);

    Paint   mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(2);

        GeoPoint gP1 = new GeoPoint(19240000,-99120000);
        GeoPoint gP2 = new GeoPoint(37423157, -122085008);

        Point p1 = new Point();
        Point p2 = new Point();

    Path    path = new Path();

    Projection  projection.toPixels(gP1, p1);
        projection.toPixels(gP2, p2);

        path.moveTo(p2.x, p2.y);
        path.lineTo(p1.x,p1.y);

        canvas.drawPath(path, mPaint);
    }

礼貌:在谷歌地图上画一条线/路径

2)这对我有用:

createMarkers()
{ 
    for(elem:bigList)
    { 
        GeoPoint geoPoint = new GeoPoint((int)(elem.getLat()*1000000), (int) (elem.getLon()*1000000)); 
        OverlayItem overlayItem = new OverlayItem(geoPoint, elem.getName(), elem.getData()); 
        itemizedOverlay.addOverlay(overlayItem); 
    } 

    itemizedOverlay.populateNow(); 
    mapOverlays.add(itemizedOverlay); //outside of for loop 
} 

在 MyOverlay 中:

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

public void populateNow()
{
    populate(); 
}

礼貌:stackoverflow.com 未知链接

3)最好的方法是使用计时器类。此链接中给出了计时器类的非常详细的描述以及如何使用它:

http://life.csu.edu.au/java-tut/essential/threads/timer.html

4)我使用了这段代码:

if(!mapOverlays.isEmpty()) 
{ 
    mapOverlays.clear(); 
    mapView.invalidate(); 
} 

希望这些答案至少对另一个人有所帮助。谢谢。

于 2010-05-24T02:10:24.613 回答
2

我也有同样的问题。我们正在同时开发一个 iphone 应用程序和一个 android 应用程序。我有 2500 多个地图叠加层。iphone上没问题;添加所有叠加层后调用 populate() 会对 android 造成巨大的性能影响。(当然,我的第一次尝试是每次添加叠加层后都调用 populate();由于 google 的教程,这是一个典型的错误。现在我只调用 populate() 一次,毕竟 2500 多个叠加层已添加到 ItemizedOverlay。 )

因此,在 htc hero 设备上,单个 populate() 调用需要 40 多秒才能完成。我不得不设置一个忙碌的指标;不可能有进度条,因为我们无法获得有关 populate() 进度的任何信息。

我尝试了另一种解决方案:不使用 ItemizedOverlay,而是手动添加覆盖,子类化 Overlay。结果:确实将所有这些叠加层添加到地图要快得多;但是,由于在每个叠加层上不断调用 draw() 方法,地图变得不可用。在我的draw方法中,我尽量优化;我不会每次都创建位图。我的 draw() 方法如下所示:

public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow)  {
// our marker bitmap already includes shadow.
// centerPoint is the geopoint where we need to put the marker.
 if (!shadow) {
  Point point = new Point();  
  mapView.getProjection().toPixels(centerPoint, point);
  canvas.drawBitmap(markerBitmap, point.x, point.y, null);
 }

}

这里 markerBitmap 是预先计算好的。我不知道我还能优化什么。如果我们不使用 ItemizedOverlay,是否需要某种 populate() 调用???我找不到任何好的答案。

现在,一旦在后台线程中创建了 ItemizedOverlay,我就求助于缓存它。这样至少用户不必每次都等待。

于 2011-01-28T14:04:03.807 回答
0

对于#2,我认为您在那里没有解决任何问题。您难以阅读的代码显示了如何在叠加层上放置标记,然后如何将该叠加层添加到地图中。我就是这样做的。我有大约 300 家酒店的地图,我的 Nexus One 上的 Android 需要大约 5 秒来创建标记。整个事情都在线程内运行,我想我将不得不做一些进度条让用户知道发生了什么。

我正在开发 iPhone 上已经存在的应用程序,似乎 iPhone 几乎没有任何问题可以立即绘制这 300 多个标记。我很难向我的老板解释进度条的存在。

如果有人知道如何优化这个过程......我将不胜感激。

这是我的代码:

    ...
        for (int m = 0; m < ArrList.size(); m++) {
            tName = ArrList.get(m).get("name").toString();
            tId = ArrList.get(m).get("id").toString();
            tLat = ArrList.get(m).get("lat").toString();;
            tLng = ArrList.get(m).get("lng").toString();;
            try {
                lat = Double.parseDouble(tLat);
                lng = Double.parseDouble(tLng);
                p1 = new GeoPoint(
                        (int) (lat * 1E6), 
                        (int) (lng * 1E6));
                OverlayItem overlayitem = new OverlayItem(p1, tName, tId);
                itemizedoverlay.addOverlay(overlayitem);
            } catch (NumberFormatException e) {
                Log.d(TAG, "NumberFormatException" + e);    
            }
        } 

我知道我可以通过避免这种 String > Double 转换来节省一些时间,但我觉得这不会给我带来显着的节省......或者它会?

于 2011-01-14T22:32:36.473 回答
-2

对于您的第四个问题....只需使用 mapOverlays.clear(); 方法和所有以前的标记将消失。

代码:

if(!mapOverlays.isEmpty()) { 
    mapOverlays.clear();
    mapView.invalidate();
}
于 2012-10-26T06:52:00.193 回答
-3

可以将多个可绘制对象添加到单个叠加层,然后可以将其添加到地图中。因此,除非对象属于不同类型,否则不需要为 x 个对象绘制 x 个叠加层。代码片段.. .. 这CustomPinPoint是我的扩展类ItemizedOverlay<OverlayItem>

CustomPinPoint customPinPoint = new CustomPinPoint(drawable, Main.this);

OverlayItem tempOverLayItem = new OverlayItem(
    touchedPoint, "PinPoint", "PintPoint 2"); //Point One
customPinPoint.insertPinPoint(tempOverLayItem);
tempOverLayItem = new OverlayItem(new GeoPoint(
        (int)(-27.34498 * 1E6), (int)(153.00724 * 1E6)), "PinPoint",
    "PintPoint 2"); //Point Two
customPinPoint.insertPinPoint(tempOverLayItem);
overlayList.add(customPinPoint); //Overlay added only once
于 2012-05-08T15:50:05.263 回答