Android Google地图中高效的地图覆盖
我想做以下几件事,有几天我一直在做这些事情:Android Google地图中高效的地图覆盖,android,google-maps,google-maps-markers,Android,Google Maps,Google Maps Markers,我想做以下几件事,有几天我一直在做这些事情: 我试图绘制多段线(我对多段线进行了编码,但成功地对其进行了解码)移动地图时移动的多段线 我找到的唯一解决方案是将地质点转换为屏幕坐标。。。如果我移动地图,它就不会移动 添加大约150个标记,速度非常慢 知道怎么办吗?我在考虑线程(处理器) 我在寻找某种计时器函数,它周期性地执行给定的函数,比如说,每1分钟左右执行一次 我还在寻找清除谷歌地图上所有标记/线条的方法 答复如下: 1) 以下是我使用的解决方案: /** Called when the ac
我找到的唯一解决方案是将地质点转换为屏幕坐标。。。如果我移动地图,它就不会移动
/** 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);
}
在MyOverlay:
public void addOverlay(OverlayItem overlay)
{
m_overlays.add(overlay);
}
public void populateNow()
{
populate();
}
提供:stackoverflow.com未知链接
3) 最好的方法是使用计时器类。有关计时器类及其使用方法的详细说明,请参见以下链接:
4) 我使用了以下代码:
if(!mapOverlays.isEmpty())
{
mapOverlays.clear();
mapView.invalidate();
}
希望这些答案至少能帮助其他人。谢谢。第二,我认为你没有解决任何问题。您的难读代码显示如何在覆盖上放置标记,然后如何将覆盖添加到地图。我就是这样做的。我有大约300家酒店的地图,NexusOne上的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);
}
}
。。。
对于(int m=0;m
我知道我可以通过避免这个字符串>双重转换来节省一些时间,但我觉得这不会给我带来显著的节省。。还是会呢?我也有同样的问题。我们正在同时开发一款iphone应用程序和一款android应用程序。我有2500多张地图。iphone没有问题;在添加所有覆盖后调用populate()时,android的性能受到了巨大的影响。(当然,我的第一次尝试是每次添加覆盖后都调用populate();这是一个典型的错误,因为谷歌的教程。现在我只调用了一次populate(),在所有2500多个覆盖都添加到ItemizeOverlay之后。) 因此,在htc hero设备上完成单个populate()调用需要超过40秒。我必须输入一个忙碌的指示器;由于无法获取有关populate()进度的任何信息,因此无法创建进度条 我尝试了另一种解决方案:不使用ItemizedOverlay,而是手动添加覆盖,对覆盖进行子类化。结果:事实上,将所有这些叠加添加到地图上要快得多;但是,由于在每个覆盖上不断调用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是预计算的。我不知道还能优化什么。如果我们不使用ItemizeOverlay,是否需要某种populate()调用???我找不到任何好的答案
现在,我求助于在后台线程中创建ItemizedOverlay后缓存它。这样,至少用户不必每次都等待。可以将多个可绘制对象添加到单个叠加中,然后将其添加到地图中。因此,除非对象的类型不同,否则不需要为x个对象绘制x个覆盖。代码段。。 .. 这里,
CustomPinPoint
是我的类,它扩展了ItemizedOverlay
关于你的第四个问题。。。。只需使用mapOverlays.clear();方法,所有以前的标记都将消失 代码:
问题1解决了…我在浏览stackoverflow.com时找到了答案…:)解决方法发布在这里q4…解决了,我在stackoverflow.com上找到了答案…解决方法是:…如果(!mapOverlays.isEmpty()){mapOverlays.clear();mapView.invalidate();}q2解决了,在ostackoverflow.com上找到了…//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(overlay);}itemizedOverlay.populateNow();mapOverlays.add(itemizedOverlay);//在for循环之外}和在MyOverlay中:public void addOverlay(OverlayItem overlay){m_overlays.add(overlay);}public void populateNow(){populate();}问题3已解决……在stackoverflow.com q3--更好的方法:)您应该在单独的答案中提供解决方案,而不是在注释中。这对每个人都有好处,而且它使您能够使用fo
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);
}
}
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
if(!mapOverlays.isEmpty()) {
mapOverlays.clear();
mapView.invalidate();
}