我已将我的 Android 应用程序中的 Google map v1 升级到 v2。
v2 很好等等,但有一种方法似乎是我生命中最慢的方法。
Projection proj = map.getProjection();
Point point = proj.toScreenLocation(example.m_geopoint);
toScreenLocation(...) 太慢以至于应用程序变慢到无法使用的慢动作。 该方法每帧可能更新 100 次,但在 Google Maps v1 上效果非常好。
当我检查 Android 控制台时,我看到了这个:
10-06 13:53:04.460: D/dalvikvm(4889): GC_EXPLICIT freed 251K, 14% free 14622K/16839K, paused 3ms+5ms
10-06 13:53:05.859: D/dalvikvm(4889): GC_EXPLICIT freed 252K, 14% free 14622K/16839K, paused 2ms+5ms
10-06 13:53:07.222: D/dalvikvm(4889): GC_EXPLICIT freed 251K, 14% free 14622K/16839K, paused 3ms+6ms
...
调用方法时,此消息一直出现。
v2 和 v1 的区别是:
pointOut = proj.toScreenLocation(geopointIn); // v2
projection.toPixels(geopointIn, pointOut); // v1
而 v1 似乎是更优化的解决方案。有什么方法可以更快吗?是性能错误吗?
最佳答案
我参加聚会的时间非常非常晚,但我有一个使用 android.graphics.Matrix 的答案:
float getYMercator(double latitude){
return (float)Math.log(Math.tan(Math.PI*(0.25+latitude/360.0)));
}
float [] fastToScreenLocation(GoogleMap map, List<LatLng> coordinates,int viewHeight, int viewWidth){
VisibleRegion visibleRegion = map.getProjection().getVisibleRegion();
Matrix transformMatrix = new Matrix();
boolean ok = transformMatrix.setPolyToPoly(
new float[] {
(float)visibleRegion.farLeft.longitude, getYMercator(visibleRegion.farLeft.latitude),
(float)visibleRegion.farRight.longitude, getYMercator(visibleRegion.farRight.latitude),
(float)visibleRegion.nearRight.longitude, getYMercator(visibleRegion.nearRight.latitude),
(float)visibleRegion.nearLeft.longitude, getYMercator(visibleRegion.nearLeft.latitude)
},0,
new float[] {
0, 0,
viewWidth, 0,
viewWidth,viewHeight,
0, viewHeight
}, 0,
4);
if(!ok) return null;
float[] points = new float[2*coordinates.size()];
for(int i=0;i<coordinates.size();++i){
LatLng location = coordinates.get(i);
points[2*i]=(float)location.longitude;
points[2*i+1]=getYMercator(location.latitude);
}
transformMatrix.mapPoints(points);
return points;
}
基本上,它将 LatLng 投影到伪 WebMercator 坐标(WebMercator 坐标线性转换以节省一些计算)。然后计算将可见区域转换为屏幕坐标所需的矩阵,并将找到的矩阵应用于坐标列表中的点。与我在三星 J3 上对 150 个坐标列表进行的测试相比,速度提高了大约 300 倍(避免为多个调用重新分配数组)。
关于java - Google Maps v2 Projection.toScreenLocation(...) 极慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26215787/