5

2 つの場所の間のパスを描画できますが、距離が長すぎる場合 (300 キロメートル以上)、パスが完全に描画されません。

パスを描画するために、以下のコードを使用しています。

class MapOverlay extends com.google.android.maps.Overlay {
    Road mRoad;
    ArrayList<GeoPoint> mPoints;

    public MapOverlay(Road road, MapView mv) {
            mRoad = road;
            if (road.mRoute.length > 0) {
                    mPoints = new ArrayList<GeoPoint>();
                    for (int i = 0; i < road.mRoute.length; i++) {
                            mPoints.add(new GeoPoint((int) (road.mRoute[i][1] * 1000000),
                                            (int) (road.mRoute[i][0] * 1000000)));
                    }
                    int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
                                    mPoints.size() - 1).getLatitudeE6() - mPoints.get(0)
                                    .getLatitudeE6()) / 2);
                    int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints.get(
                                    mPoints.size() - 1).getLongitudeE6() - mPoints.get(0)
                                    .getLongitudeE6()) / 2);
                    GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong);

                    MapController mapController = mv.getController();
                    mapController.animateTo(moveTo);
                    mapController.setZoom(8);
            }
    }

    @Override
    public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
            super.draw(canvas, mv, shadow);
            drawPath(mv, canvas);
            return true;
    }

    public void drawPath(MapView mv, Canvas canvas) {
            int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
            Paint paint = new Paint();
            paint.setColor(Color.GREEN);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(3);
            for (int i = 0; i < mPoints.size(); i++) {
                    Point point = new Point();
                    mv.getProjection().toPixels(mPoints.get(i), point);
                    x2 = point.x;
                    y2 = point.y;
                    if (i > 0) {
                            canvas.drawLine(x1, y1, x2, y2, paint);
                    }
                    x1 = x2;
                    y1 = y2;
            }
    }

}

4

3 に答える 3

2

まず、廃止された古い V1 API ではなく、Google Maps API V2 を使用する必要があります。

アクティビティで、Google マップとポリラインの参照を作成します。

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback{

    private GoogleMap mMap;
    Polyline line;
    //......

最初にウェイポイント リストを定義します。

List<LatLng> latLngWaypointList = new ArrayList<>();

ルートを取得し、ルートのポリラインを描画してから、ウェイポイント マーカーを描画します。

class GetDirectionsAsync extends AsyncTask<LatLng, Void, List<LatLng>> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected List<LatLng> doInBackground(LatLng... params) {
           List<LatLng> route = new ArrayList<>();
           //populate route......
           return route;
    }

    @Override
    protected void onPostExecute(List<LatLng> route) {

        if (route == null) return;

        if (line != null){
            line.remove();
        }

        PolylineOptions options = new PolylineOptions().width(5).color(Color.MAGENTA).geodesic(true);
        for (int i = 0; i < pointsList.size(); i++) {
            LatLng point = route.get(i);
            //If this point is a waypoint, add it to the list
            if (isWaypoint(i)) {
                latLngWaypointList.add(point);
            }

            options.add(point);
        }
        //draw the route:
        line = mMap.addPolyline(options);

        //draw waypoint markers:
        for (LatLng waypoint : latLngWaypointList) {
            mMap.addMarker(new MarkerOptions().position(new LatLng(waypoint.latitude, waypoint.longitude))
                    .title("Waypoint").icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_VIOLET)));
        }

    }
}

isWaypoint() の単なるプレースホルダー実装を次に示します。

public boolean isWaypoint(int i) {
    //replace with your implementation
    if (i % 17 == 0) {
        return true;
    }
    return false;
}

西海岸から東海岸まで約 2500 マイルのルートの結果:

ここに画像の説明を入力

より小さいルートの結果:

ここに画像の説明を入力

この例では、ルートを道路にスナップさせるために Google Directions API も使用していることに注意してください。Google Directions API の使用方法の完全な例については、https ://stackoverflow.com/a/32940175/4409409 を参照してください。

于 2016-04-07T01:50:45.310 に答える
-1

私が間違っていなければ、グーグルマップでパスを描くのはとても簡単です。 ソースコード

var mumbai = new google.maps.LatLng(18.9750, 72.8258);
        var mapOptions = {
            zoom: 8,
            center: mumbai
        };
        map = new google.maps.Map(document.getElementById('dmap'), mapOptions);
        directionsDisplay.setMap(map);
        source="18.9750, 72.8258";
       destination= "18.9550, 72.8158";
        source = (source1.replace(" ", ","));
        destination = (destination1.replace(" ", ","));
        var request = {
            origin: source,
            destination: destination,
            travelMode: google.maps.TravelMode.DRIVING
        };
        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }
        });
于 2016-04-12T13:06:24.930 に答える