Грешни разстояния при създаване на произволни местоположения

Опитвам се да създам произволни местоположения близо до моето местоположение. Това, което искам, е да създам произволни двойки географска ширина/дължина в кръг от 200 метра около моето местоположение. След като зададох този въпрос: генериране на произволни местоположения в близост до моето местоположение това е, което имам идва с.

в 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;
}

Работата е там, че се случва нещо странно, защото всички произволни местоположения са твърде близо до центъра на местоположението ми, изглежда, че формулата не покрива целия радиус и мисля, че разстоянията не са в реални метри.

Някаква идея какво може да не е наред с формулата ми?

Благодаря предварително


person pindleskin    schedule 21.05.2012    source източник
comment
Не съм експерт по картографски проекции, но тези хора може да са: gis.stackexchange.com   -  person Tony the Pony    schedule 21.05.2012
comment
Благодаря ти Тони, тази връзка наистина ми помогна. Ако напишете отговор с него, ще го маркирам като приет   -  person pindleskin    schedule 22.05.2012


Отговори (2)


За въпроси относно картографски проекции и координатни изчисления този SE сайт може да бъде полезен: http://gis.stackexchange.com (Географски информационни системи).

person Tony the Pony    schedule 22.05.2012

За да сте сигурни, използвайте формулата distanceTo() на клас местоположение. Можете да го направите по следния начин:

double distance  = a.distanceTo(b);

където a и b са обекти за местоположение.

person NiVeR    schedule 21.05.2012
comment
Благодаря ти. След проверка разстоянията са в реални метри. Сега изглежда, че не създавам местоположения, използвайки целия радиус, а половината от него - person pindleskin; 21.05.2012