Google Maps v2 Projection.toScreenLocation(…) extremely slow

后端 未结 2 660
悲&欢浪女
悲&欢浪女 2021-02-15 18:13

I have upgraded Google Maps v1 to v2 in my Android application.

And v2 was nice and so on BUT one method seems to be the slowest thing in my life.

Projec         


        
2条回答
  •  庸人自扰
    2021-02-15 18:29

    I come very, very late to the party, but I have an answer using 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 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

    Basically, it projects the LatLng into pseudo WebMercator coordinates (WebMercator coordinates linearly transformed to save some calculations). Then it calculates the necessary matrix to transform the visible region to screen coordinates and applies the found matrix to the points in the coordinates list. Roughly 300x faster from my tests for a list of 150 coordinates on my Samsung J3 (avoiding array reallocation for multiple calls).

提交回复
热议问题