【问题标题】:Is there any limit to drawing path using Geocoder (Android)?使用 Geocoder (Android) 绘制路径是否有任何限制?
【发布时间】:2011-02-18 10:45:22
【问题描述】:

我可以在两个位置之间绘制一条路径,但如果距离太长 - 超过 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;
            }
    }

}

【问题讨论】:

  • 您使用的是谷歌地图 api 吗?当您尝试 300 公里的路线时,您的出发地和目的地是什么?
  • 不,使用地理编码器的绘制路径没有任何限制。使用 Google Maps Directions API。您可以参考 [developers.google.com/maps/documentation/directions/].
  • 所以回答我的问题。您的程序在较小的距离下是否可以正常工作?

标签: android google-maps overlay shape android-maps


【解决方案1】:

首先,您应该使用 Google Maps API V2 而不是旧的已弃用的 V1 API

在 Activity 中,创建 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

【讨论】:

  • @ravi 你能更准确地解释一下你想要实现的目标吗?你有你想要的 UI 模型吗?
【解决方案2】:
  • 我google direction api 你没有距离限制 想要路线。您还有其他限制,例如您可以在 24 小时内提出的请求数量等。详情请访问here
  • 在您的 android 应用开发中始终使用最新版本的 google maps api 和 play services。
  • 按照 api 指南中的指南进行编程,从而减少出错的机会。
  • 所以接下来你在绘制距离时遇到了问题,该距离相当大,例如 300 公里。我假设您可以轻松地绘制较短距离的路径,并且您对此没有任何问题。考虑到这个条件(你可以轻松地做短距离)让我们开始-

解决方案

  1. 您在绘制较大路径时遇到的问题是由于大 路点的数量,这导致对堆的更大需求 应用程序的内存。
  2. 因此,android 内核在运行时为应用程序提供了一小块内存用于暂存内存。但这对于较大的路线是不够的,在更糟糕的情况下,您的应用会挂起或崩溃。
  3. 对我有用的解决方案是向 android 内核显式请求更多堆内存。您可以在清单中执行此操作。

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:largeHeap="true"
    .........
    

    请看行

    android:largeHeap="true"

    这是您需要在代码中执行的操作。

希望这个解释对您的问题有所帮助。

【讨论】:

    【解决方案3】:

    如果我没记错的话,在谷歌地图中绘制路径非常简单。 源代码

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      相关资源
      最近更新 更多