2016-12-24 36 views
0

我想一个谷歌静态地图上显示之间有两个地方路线,但是当两地间的距离图像显示是这样的: - images show like that如何显示在谷歌静态地图全航线的Android

我想说明两点。如何以编程方式正确缩小。

请帮帮我。提前致谢。

+0

您可以使用可见参数定义静态地图的视口。 详情请参阅文档: https://developers.google.com/maps/documentation/static-maps/intro#Viewports – xomena

+0

我有同样的问题,这就是我所做的:1.我添加了拉脱维亚人,然后平均得到中点。 2.然后我计算两点之间的距离3.然后我使用轨迹方法根据距离之间的距离创建最佳缩放级别。因此,我有一个switch语句根据点之间的距离选择缩放级别。然后,我在摄像头上应用摄像头缩放 – Mushirih

+0

@PeterMushirih你可以举一些例子或者解释一些。 –

回答

0

我想你使用的是折线这样

绘制你的路,所以我说的功能,所以我现在缩小基于之间的距离来计算距离

private class ParserTask extends AsyncTask<String,Integer,List<List<HashMap<String,String>>>>{ 
     JSONObject jsonObject; 
     List<List<HashMap<String,String>>> routes=null; 

     @Override 
     protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) { 
      try { 
       jsonObject=new JSONObject(jsonData[0]); 
       PathJsonParser pathJsonParser=new PathJsonParser(); 
       routes=pathJsonParser.parse(jsonObject); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return routes; 
     } 

     @Override 
     protected void onPostExecute(List<List<HashMap<String, String>>> lists) { 
      ArrayList<LatLng> points = null; 
      PolylineOptions polyLineOptions = null; 
     if(routes.size()>0){ 
      // traversing through routes 
      for (int i = 0; i < routes.size(); i++) { 
       points = new ArrayList<LatLng>(); 
       polyLineOptions = new PolylineOptions(); 
       List<HashMap<String, String>> path = routes.get(i); 

       for (int j = 0; j < path.size(); j++) { 

        HashMap<String, String> point = path.get(j); 

        double lat = Double.parseDouble(point.get("lat")); 
        double lng = Double.parseDouble(point.get("lng")); 
        LatLng position = new LatLng(lat, lng); 
        points.add(position); 
       } 

       polyLineOptions.addAll(points); 
       polyLineOptions.width(10); 
       polyLineOptions.color(Color.BLUE); 
      } 
     } 
      if(null!=polyLineOptions) { 
       mMap.addPolyline(polyLineOptions); 
       float totalDistance = 0; 
       for(int k = 1; k < polyLineOptions.getPoints().size(); k++) { 
        Location currLocation = new Location("this"); 
        currLocation.setLatitude(polyLineOptions.getPoints().get(k).latitude); 
        currLocation.setLongitude(polyLineOptions.getPoints().get(k).longitude); 
        Location lastLocation = new Location("that"); 
        lastLocation.setLatitude(polyLineOptions.getPoints().get(k-1).latitude); 
        lastLocation.setLongitude(polyLineOptions.getPoints().get(k-1).longitude); 
        totalDistance += lastLocation.distanceTo(currLocation); 
       } 
       DISTANCE_BETWEEN= (int) (totalDistance/1000); 
      } 
     } 
    } 

,我缩小从这两点的中点开始

  LatLng x = new LatLng((LOCATION_FROM.latitude + LOCATION_TO.latitude)/2, (LOCATION_FROM.longitude + LOCATION_TO.longitude)/2); 
      mMap.resetMinMaxZoomPreference(); 
      if (DISTANCE_BETWEEN < 30) { 
       CAMERA_ZOOM = 8; 
      } else if (DISTANCE_BETWEEN > 30 && DISTANCE_BETWEEN < 60) { 
       CAMERA_ZOOM = 6; 
      } else { 
       CAMERA_ZOOM = 4; 
      } 
      CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(x, CAMERA_ZOOM); 
      mMap.animateCamera(cameraUpdate);