根据缩放级别如何获取像素数等于地球圆周的半径?或区圈的半径来屏幕像素? [英] How to get number of pixels equals to a radius of Geozone circle according to zoom level? OR Radius of zone circle to screen pixels?

查看:245
本文介绍了根据缩放级别如何获取像素数等于地球圆周的半径?或区圈的半径来屏幕像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在谷歌地图上创建Geozones。地图API提供的默认圈子在滚动地图时会出现laggy。所以,我想创建自己的圈子(覆盖地图)。我创建了半径为300米的固定圆。此圆圈的宽度应根据缩放级别作为默认地图圆圈进行更改。我写了一个如下的方法,通过将半径和缩放级别作为参数来计算像素。我还添加了默认的圆圈来验证我的圈子对于不同地址的准确性。

  //根据半径和缩放级别调整圆的宽度和高度
private void adjustCircleWidth(final LatLng center ,最终双倍缩放){
//通过https://developers.google.com/maps/documentation/android-api/views
double widthOfWorldDP = 256 * Math中的主题Zoom输入。 pow(2,zoom);

int radius = 300; //米。
int circleWidthMeters = radius * 2; // * 2获取直径或宽度。

//从这个SOF文章的google和alsop http://stackoverflow.com/a/6452332/3209739
int EQUATOR_LENGTH_METERS = 40075004; //以米为单位..

float dpOfCircle =(float)((widthOfWorldDP * circleWidthMeters)/ EQUATOR_LENGTH_METERS);

//根据设备密度将dp转换为像素。

mCircleView.requestLayout();
mCircleView.getLayoutParams()。height = pixels;
mCircleView.getLayoutParams()。width = pixels;
}

好的,这工作正常。我开始验证其他位置,然后开始显示变化。圆圈宽度小于地图的默认圆圈。我分析它的理解是,它显示完美(与默认圆圈同步),当我将圆圈设置在赤道上的任何位置时。由于考试圆圈与肯尼亚的默认值完全同步,因为肯尼亚位于赤道上。看到附件scrrenshot。如果我继续离开赤道,圆圈会从默认圆圈变得越来越小。对于德国来说,因为位置圈几乎是默认圈的一半......请参阅附加的scrrenshot。

这是因为我使用的公式与赤道有关。



您可以提供任何有关如何使其统一的线索吗? >

致谢并感谢大量<​​a href=\"https://stackoverflow.com/a/14428226/3209739\"> Android Maps API v2绘制圈帖子。

我可以以米为单位计算以纬度,经度和半径(从给定lat,long)开始的以像素为单位的圆圈宽度。

  private int convertZoneRadiusToPixels(double lat,double lng,double radiusInMeters){
double EARTH_RADIUS = 6378100.0;
double lat1 = radiusInMeters / EARTH_RADIUS;
double lng1 = radiusInMeters /(EARTH_RADIUS * Math.cos((Math.PI * lat / 180)));

double lat2 = lat + lat1 * 180 / Math.PI;
double lng2 = lng + lng1 * 180 / Math.PI;

Point p1 = mMap.getProjection()。toScreenLocation(new LatLng(lat,lng));
Point p2 = mMap.getProjection()。toScreenLocation(new LatLng(lat2,lng2));
返回Math.abs(p1.x - p2.x);
}


I am creating Geozones on google map. Default Circle that map API provides is laggy when scroll the map. So, I wanted to create my own circle (overlay on map). I created circle with radius of 300 meters which is fixed. The width of this circle should change according to the zoom level as the default map circle does. I have writen a method as below that calculates pixels by taking Radius and Zoom level as parameters. I have added the default circle also to verify the accuracy of my circle for different addresses.

//Adjusts the width and height of circle according to radius and zoom level
    private void adjustCircleWidth(final LatLng center, final double zoom) {
        //With inputs from topic "Zoom" from https://developers.google.com/maps/documentation/android-api/views
        double widthOfWorldDP = 256 * Math.pow(2, zoom);

        int radius = 300; //meters.
        int circleWidthMeters = radius * 2;  // *2 to get diameter or width.

        //From google and alsop from this SOF post http://stackoverflow.com/a/6452332/3209739
        int EQUATOR_LENGTH_METERS = 40075004; //in meters..

        float dpOfCircle = (float) ((widthOfWorldDP * circleWidthMeters) / EQUATOR_LENGTH_METERS);

        //converts dp to pixels depending on device density..
        int pixels = (int) convertDpToPixels(dpOfCircle);

        mCircleView.requestLayout();
        mCircleView.getLayoutParams().height = pixels;
        mCircleView.getLayoutParams().width = pixels;
    }

Well, this works fine. I started verifying for other locations then it started showing variations. Circle width is smaller than the map's default circle. I analysed it to understand that it show perfectly (in sync with default circle) when I set the circle around any location that is on Equator..for exampple circle is in perfect sync with default for Kenya as location as Kenya is on equator..see the attached scrrenshot. If I keep move away from equator, the circle goes smaller and smaller from the default circle. Say, for Germany as location circle is almost half of the default one...see the attached scrrenshot.

This is because formula I used is tied to equator.

Can you help with any clue on how can I make it uniform ?

解决方案

Credits to and thanks a ton to Android Maps API v2 draw circle post.

I could calculate the width of circle in pixels given latitude, longitude and radius (from given lat, long) in meters.

private int convertZoneRadiusToPixels(double lat, double lng, double radiusInMeters) {
    double EARTH_RADIUS = 6378100.0;
    double lat1 = radiusInMeters / EARTH_RADIUS;
    double lng1 = radiusInMeters / (EARTH_RADIUS * Math.cos((Math.PI * lat / 180)));

    double lat2 = lat + lat1 * 180 / Math.PI;
    double lng2 = lng + lng1 * 180 / Math.PI;

    Point p1 = mMap.getProjection().toScreenLocation(new LatLng(lat, lng));
    Point p2 = mMap.getProjection().toScreenLocation(new LatLng(lat2, lng2));
    return Math.abs(p1.x - p2.x);
}

这篇关于根据缩放级别如何获取像素数等于地球圆周的半径?或区圈的半径来屏幕像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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