2

私は自分の場所の近くにランダムな場所を作成しようとしています。私が欲しいのは、私の場所を囲む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;
}

ランダムな場所がすべて私の場所の中心に近すぎるため、何か奇妙なことが起こっています。数式は半径全体をカバーしていないようで、距離は実際のメートルではないと思います。

私の数式で何が間違っている可能性があるかについて何か考えはありますか?

前もって感謝します

4

2 に答える 2

1

地図投影法座標計算に関する質問については、このSEサイトが役立つ可能性があります:http://gis.stackexchange.com(地理情報システム)。

于 2012-05-22T09:41:08.453 に答える
0

必ずロケーションクラスのdistanceTo()式を使用してください。次のように実行できます。

double distance  = a.distanceTo(b);

ここで、aとbはLocationオブジェクトです。

于 2012-05-21T10:54:02.477 に答える