android - 创建随机位置时距离错误

标签 android geolocation range distance

我正在尝试在我的位置附近创建随机位置。我想要的是在我所在位置周围 200 米的圆内创建随机纬度/经度对。问了这个问题后:generate random locations nearby my location这就是我的想法。

在 onLocationChanged 中,这就是我所做的:

private void updateWithNewLocation(Location location) {
    if (location != null) {
        this.myItemizedOverlay.clear();
        this.nonPlayerItemizedOverlay.clear();
        this.mapOverlays.clear();
        // Update my map location.
        Double latitude = location.getLatitude() * 1E6;
        Double longitude = location.getLongitude() * 1E6;
        GeoPoint geoPoint = new GeoPoint(latitude.intValue(),
                longitude.intValue());

        CustomOverlayItem pcOverlayItem = new CustomOverlayItem(geoPoint,
                "", "", "");

        this.mapController.animateTo(geoPoint);

        this.myItemizedOverlay.setLocation(location);
        this.myItemizedOverlay.addOverlay(pcOverlayItem);
        this.mapOverlays.add(this.myItemizedOverlay);

        // Everytime i get a new location i generate random non player
        // characters near my location
        int numberOfNonPlayers = new Random().nextInt(5);

        for (int i = 0; i < numberOfNonPlayers; i++) {
            int radius = (int) this.myMapView.getProjection()
                    .metersToEquatorPixels(200);
            double lowerLimit = -1;
            double upperLimit = 1;

            Double newLatitude = location.getLatitude()
                    * 1E6
                    + (radius * (lowerLimit + (Math.random() * ((upperLimit - lowerLimit) + 1))));
            Double newLongitude = location.getLongitude()
                    * 1E6
                    + (radius * (lowerLimit + (Math.random() * ((upperLimit - lowerLimit) + 1))));

            GeoPoint geoPoint2 = new GeoPoint(newLatitude.intValue(),
                    newLongitude.intValue());

            CustomOverlayItem npcOverlayItem = new CustomOverlayItem(
                    geoPoint2, "", "", "");

            Location newLocation = new Location("npc " + i);
            newLocation.setLatitude(newLatitude);
            newLocation.setLongitude(newLongitude);

            this.nonPlayerItemizedOverlay = new NonPlayerItemizedOverlay(
                    this.nonPlayerDrawable, this.myMapView);
            this.nonPlayerItemizedOverlay.setLocation(newLocation);
            this.nonPlayerItemizedOverlay.addOverlay(npcOverlayItem);
            this.mapOverlays.add(this.nonPlayerItemizedOverlay);
        }
    }
}

这是我的 NonPlayerItemizedOverlay 类:

public class NonPlayerItemizedOverlay extends BaseItemizedOverlay {

public NonPlayerItemizedOverlay(Drawable defaultMarker, MapView mapView) {
    super(defaultMarker, mapView);
    // TODO Auto-generated constructor stub
}

private Location location;

public static int metersToRadius(float meters, MapView map, double latitude) {
    return (int) (map.getProjection().metersToEquatorPixels(meters) * (1 / Math
            .cos(Math.toRadians(latitude))));
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    super.draw(canvas, mapView, shadow);

    if (shadow == false) {
        Projection projection = mapView.getProjection();

        // Get the current location
        Double latitude = location.getLatitude();
        Double longitude = location.getLongitude();
        GeoPoint geoPoint = new GeoPoint(latitude.intValue(),
                longitude.intValue());

        // Convert the location to screen pixels
        Point point = new Point();
        projection.toPixels(geoPoint, point);

        // int radius = metersToRadius(30, mapView, latitude);
        int radius = (int) mapView.getProjection()
                .metersToEquatorPixels(50);

        RectF oval = new RectF(point.x - radius, point.y - radius, point.x
                + radius, point.y + radius);

        // Setup the paint
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(2.0f);

        paint.setColor(0xffE62020);
        paint.setStyle(Style.STROKE);
        canvas.drawOval(oval, paint);

        paint.setColor(0x18E62020);
        paint.setStyle(Style.FILL);
        canvas.drawOval(oval, paint);
    }
}

public Location getLocation() {
    return location;
}

public void setLocation(Location location) {
    this.location = location;
}

还有我的 MyItemizedOverlay 类:

public class MyItemizedOverlay extends BaseItemizedOverlay {

public MyItemizedOverlay(Drawable defaultMarker, MapView mapView) {
    super(defaultMarker, mapView);
    // TODO Auto-generated constructor stub
}

private Location location;

public static int metersToRadius(float meters, MapView map, double latitude) {
    return (int) (map.getProjection().metersToEquatorPixels(meters) * (1 / Math
            .cos(Math.toRadians(latitude))));
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    super.draw(canvas, mapView, shadow);

    if (shadow == false) {
        Projection projection = mapView.getProjection();

        // Get the current location
        Double latitude = location.getLatitude() * 1E6;
        Double longitude = location.getLongitude() * 1E6;
        GeoPoint geoPoint = new GeoPoint(latitude.intValue(),
                longitude.intValue());

        // Convert the location to screen pixels
        Point point = new Point();
        projection.toPixels(geoPoint, point);

        // int radius = metersToRadius(100, mapView, latitude);
        int radius = (int) mapView.getProjection().metersToEquatorPixels(
                200);

        RectF oval = new RectF(point.x - radius, point.y - radius, point.x
                + radius, point.y + radius);

        // Setup the paint
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(2.0f);

        paint.setColor(0xff6666ff);
        paint.setStyle(Style.STROKE);
        canvas.drawOval(oval, paint);

        paint.setColor(0x186666ff);
        paint.setStyle(Style.FILL);
        canvas.drawOval(oval, paint);
    }
}

public Location getLocation() {
    return location;
}

public void setLocation(Location location) {
    this.location = location;
}

事情是一些奇怪的事情发生了,因为所有的随机位置都离我的位置中心太近了,似乎公式没有覆盖整个半径,我认为距离不是真正的米。

知道我的公式有什么问题吗?

提前致谢

最佳答案

有关 map 投影坐标计算的问题,此 SE 站点可能会有所帮助:http://gis.stackexchange.com (地理信息系统)。

关于android - 创建随机位置时距离错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10682743/

相关文章:

android - 如何绘制从当前位置到从 Mysql 数据库检索到的位置的路径

android - 在 Android 中双击主页按钮

java - 适用于 Android 的 Fabric crashlytics

java - 当应用程序未打开并且当我尝试在 Realm 中插入数据时,我收到错误 "Realm cannot be automatically updated on a thread > without a looper"?

Php附近的地方脚本帮助

python - 使用 Win32com Python 中的 Range 和 Union 方法

php - 从他们的 IP 获取访问者所在的国家/地区

java - java 中的制图和路线图

javascript - 从范围输入( slider )获取值

python - 检查字符串是否在列表中,具体取决于最后两个字符