高效的地图叠加在Android的谷歌地图 [英] Efficient Map Overlays in Android Google Map

查看:162
本文介绍了高效的地图叠加在Android的谷歌地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做到以下几点和我对这些样卡了好几天:

  1. 我是想为绘制折线(我有恩codeD折线,而是设法去$ C C的$)的这一举动,当我移动地图
    我发现的唯一的解决办法是Geopoints要转换为屏幕坐标,如果我移动地图将不会移动。

  2. 我用 HelloItemizedOverlay 添加关于 150标记,它会非常非常慢
    任何想法怎么办?我在想线程(处理器)。

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

  4. 我也在寻找各种方法来清除了谷歌地图的所有标记/行等。

解决方案

下面给出答案:

1),下面是我用了一个解决方案:

  / **第一次创建活动时调用。 * /
私人名单,其中,叠加> mapOverlays;

私人投影的投影;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    的LinearLayout =(的LinearLayout)findViewById(R.id.zoomview);
    图形页面=(图形页面)findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(真正的);

    mapOverlays =调用MapView.getOverlays();
    投影= MapView.getProjection()在;
    mapOverlays.add(新MyOverlay());

}

@覆盖
保护的布尔isRouteDisplayed(){
    返回false;
}

类MyOverlay扩展覆盖{

    公共MyOverlay(){

    }

    公共无效画(油画画布,图形页面mapv,布尔影子){
        super.draw(帆布,mapv,阴影);

    油漆mPaint =新的油漆();
        mPaint.setDither(真正的);
        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 =新的GeoPoint(19240000,-99120000);
        的GeoPoint GP2 =新的GeoPoint(37423157,-122085008);

        点P1 =新的点();
        点P2 =新的点();

    路径path =新路径();

    投影projection.toPixels(GP1,P1);
        projection.toPixels(GP2,P2);

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

        canvas.drawPath(路径,mPaint);
    }
 

礼遇:借鉴谷歌地图线/路径

2)这里就是为我工作:

  createMarkers()
{
    对于(ELEM:bigList)
    {
        GeoPoint对象的GeoPoint =新的GeoPoint((INT)(elem.getLat()* 1000000),(INT)(elem.getLon()* 1000000));
        OverlayItem overlayItem =新OverlayItem(的GeoPoint,elem.getName(),elem.getData());
        itemizedOverlay.addOverlay(overlayItem);
    }

    itemizedOverlay.populateNow();
    mapOverlays.add(itemizedOverlay); //为外循环
}
 

和在MyOverlay:

 公共无效addOverlay(OverlayItem叠加)
{
    m_overlays.add(叠加);
}

公共无效populateNow()
{
    填充();
}
 

礼遇:stackoverflow.com不明链接

3)的最佳方法是使用一个计时器类。 Timer类的很详细的说明,以及如何使用它被赋予在此链接:

<一个href="http://life.csu.edu.au/java-tut/essential/threads/timer.html">http://life.csu.edu.au/java-tut/essential/threads/timer.html

4)我用这个code:

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

希望这些答案可以帮助用于至少一个其他人。谢谢你。

I want to do the following and am kind of stuck on these for a few days:

  1. I was trying to draw polylines (I have encoded polylines, but have managed to decode those) that move when I move the map.
    The only solution that I found was for Geopoints to be transformed into screen coordinates... which won't move if I move the map.

  2. I used HelloItemizedOverlay to add about 150 markers and it gets very very slow.
    Any idea what to do? I was thinking about threads (handler).

  3. I was looking for some sort of a timer function that executes a given function periodically, say, every 1 minute or so.

  4. I was also looking for ways to clear the Google map from all the markers/lines, etc.

解决方案

Answers given below :

1) Here's a solution that I used :

/** 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);
    }

courtesy: Drawing a line/path on Google Maps

2) Here's what worked for me :

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 
} 

and in MyOverlay:

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

public void populateNow()
{
    populate(); 
}

courtesy: stackoverflow.com unknown link

3) The best way is to use a timer class. A very detailed description of the timer class and how to use it is given at this link :

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

4) I used this code :

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

Hope these answers help atleast one other person. Thanks.

这篇关于高效的地图叠加在Android的谷歌地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆