2011-02-18 87 views
6

我可以在两个位置之间绘制路径,但如果距离太长 - 超过300公里 - 路径未完全绘制。使用Geocoder(Android)绘制路径有没有限制?

我使用下面的代码,以绘制路径:

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

}

+0

你使用谷歌地图API?当你尝试300公里的路线时,你的来源和目的地是什么? – 2016-04-06 10:02:18

+0

不,没有使用地理编码器绘制路径的限制。使用** Google Maps Directions API **。您可以参考[https://developers.google.com/maps/documentation/directions/]。 – 2016-04-06 11:35:21

回答

0
  • 我谷歌API的方向,你不必对距离的限制,你 希望路由对于。你还有其他的限制,比如24小时的请求次数等等。详情请访问here
  • 始终使用最新版本的google maps api并在您的android应用程序开发中播放服务。
  • 按照api指南中的指导进行编程,以减少故障发生的几率。
  • 所以,接下来你在绘制距离很大时会遇到问题,比如300公里。我假设你可以轻松地绘制较短距离的路径,而且你没有问题。采取这一条件考虑在内(即可以很容易地做短距离)可让begin-

  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" 
    ......... 
    

    请在该行看看

    机器人:largeHeap = “真”

    这是你需要在你的代码做什么。

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

2

首先,你应该使用谷歌地图API V2而不是旧的过时V1 API

活动中,创建了谷歌地图和折线引用:

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里程:

enter image description here

较小路线的结果:

enter image description here

请注意,在此示例中,我还使用Google Directions API来使路线对齐道路。有关如何使用Google Directions API的完整示例,请参阅此处:https://stackoverflow.com/a/32940175/4409409

-1

如果我没有错它在Google地图中绘制路径非常简单。 源代码

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