Android Google Map 中的高效地图叠加
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2848189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Efficient Map Overlays in Android Google Map
提问by Ahsan
I want to do the following and am kind of stuck on these for a few days:
我想做以下事情,并且在这些事情上坚持了几天:
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.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).I was looking for some sort of a timer function that executes a given function periodically, say, every 1 minute or so.
I was also looking for ways to clear the Google map from all the markers/lines, etc.
我试图绘制当我移动地图时移动的折线(我已经编码了折线,但已经设法解码了这些折线)。
我发现的唯一解决方案是将 Geopoints 转换为屏幕坐标……如果我移动地图,它就不会移动。我曾经
HelloItemizedOverlay
添加了大约150 个标记,但它变得非常非常缓慢。
知道该怎么做吗?我在考虑线程(处理程序)。我正在寻找某种定时器函数,它定期执行给定的函数,比如每 1 分钟左右。
我也在寻找从所有标记/线等中清除谷歌地图的方法。
采纳答案by Ahsan
Answers given below :
答案如下:
1) Here's a solution that I used :
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);
}
courtesy: Drawing a line/path on Google Maps
2) Here's what worked for me :
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
}
and in MyOverlay:
并在 MyOverlay 中:
public void addOverlay(OverlayItem overlay)
{
m_overlays.add(overlay);
}
public void populateNow()
{
populate();
}
courtesy: stackoverflow.com unknown link
礼貌:stackoverflow.com 未知链接
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 :
3)最好的方法是使用定时器类。此链接提供了计时器类及其使用方法的非常详细的描述:
http://life.csu.edu.au/java-tut/essential/threads/timer.html
http://life.csu.edu.au/java-tut/essential/threads/timer.html
4) I used this code :
4)我使用了这个代码:
if(!mapOverlays.isEmpty())
{
mapOverlays.clear();
mapView.invalidate();
}
Hope these answers help atleast one other person. Thanks.
希望这些答案至少可以帮助另一个人。谢谢。
回答by winitzki
I have the same problem. We are developing an iphone app and an android app at the same time. I have 2500 + map overlays. No problem on iphone; a huge performance hit on android when calling populate() after adding all overlays. (Of course, my first try was to call populate() every time after adding an overlay; a typical mistake due to google's tutorial. Now I am calling populate() just once, after all 2500+ overlays have been added to the ItemizedOverlay.)
我也有同样的问题。我们正在同时开发一个 iphone 应用程序和一个 android 应用程序。我有 2500 多个地图叠加层。在iphone上没问题;添加所有叠加层后调用 populate() 时,对 android 的性能影响很大。(当然,我的第一次尝试是每次添加叠加层后都调用 populate();这是由于 google 教程造成的典型错误。现在我只调用了一次 populate(),毕竟 2500 多个叠加层已添加到 ItemizedOverlay。 )
So the single populate() call takes over 40 seconds to complete on an htc hero device. I had to put in a busy indicator; no progress bar is possible because we cannot get any information about the progress of populate().
因此,单个 populate() 调用需要超过 40 秒才能在 HTC 英雄设备上完成。我不得不放入一个忙碌指示器;没有进度条是可能的,因为我们无法获得有关 populate() 进度的任何信息。
I tried another solution: not use ItemizedOverlay but add overlays by hand, subclassing Overlay. Result: indeed it is much faster to add all those overlays to the map; however, the map becomes unusable due to constant calling of the draw() method on each overlay. In my draw method, I tried to optimize as much as possible; I do not create a bitmap every time. My draw() method looks like this:
我尝试了另一种解决方案:不使用 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);
}
}
Here markerBitmap is precomputed. I don't know what else I could optimize. Is there some kind of populate() call required if we are not using ItemizedOverlay??? I could not find any good answers for that.
这里的markerBitmap 是预先计算好的。我不知道我还能优化什么。如果我们不使用 ItemizedOverlay,是否需要某种 populate() 调用???我找不到任何好的答案。
Right now I resort to caching the ItemizedOverlay once it has been created in a background thread. This way at least the user does not have to wait every time.
现在,一旦在后台线程中创建了 ItemizedOverlay,我就会对它进行缓存。这样至少用户不必每次都等待。
回答by bobetko
For #2, I don't think you solved anything there. Your hard-to-read code is showing how to put markers on overlay and then, how to add that overlay to the map. That's exactly how I do it. I have map with around 300 hotels and it takes around 5 seconds for Android on my Nexus One to create markers. The whole thing is running inside thread and I guess I will have to do some sort of progress bar to let user know what's going on.
对于#2,我认为你没有解决任何问题。您难以阅读的代码展示了如何在叠加层上放置标记,然后如何将该叠加层添加到地图中。我就是这样做的。我有大约 300 家酒店的地图,Android 在我的 Nexus One 上创建标记大约需要 5 秒钟。整个事情都在线程内运行,我想我将不得不做某种进度条让用户知道发生了什么。
I am working on app that already exists on iPhone and it seems iPhone doesn't have any issues to almost instantaneously draw these 300+ markers. I'll have hard time to explain existence of progress bar to my bosses.
我正在开发 iPhone 上已经存在的应用程序,似乎 iPhone 几乎没有任何问题可以立即绘制这 300 多个标记。我将很难向我的老板解释进度条的存在。
If anybody have idea how to optimize this process... I will be grateful.
如果有人知道如何优化这个过程......我将不胜感激。
Here is my code:
这是我的代码:
...
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);
}
}
I know I could save some time by avoiding this String > Double conversion, but I don't feel that would give me significant saving.. or it would?
我知道我可以通过避免这种 String > Double 转换来节省一些时间,但我认为这不会给我带来显着的节省......或者它会吗?
回答by bhejaFry
For your 4th question.... simply use the mapOverlays.clear(); method and all the previous markers will be vanished.
对于你的第四个问题......只需使用 mapOverlays.clear(); 方法,之前的所有标记都将消失。
code:
代码:
if(!mapOverlays.isEmpty()) {
mapOverlays.clear();
mapView.invalidate();
}
回答by Sudhir
Multiple number of drawable objects can be added to a single Overlay which can then be added to the map. Hence, drawing x number of overlay's for x number of objects wouldnt be necessary unless the objects are of different types. Code snippet..
..
Here, CustomPinPoint
is my class which extends ItemizedOverlay<OverlayItem>
可以将多个可绘制对象添加到单个 Overlay,然后将其添加到地图中。因此,除非对象是不同类型的,否则不需要为 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