【问题标题】:Google Map v2 zoom-to-fit all markers AND Poly LinesGoogle Map v2 缩放以适合所有标记和折线
【发布时间】:2014-08-27 15:42:21
【问题描述】:

所以,我在我的 Android 应用程序上的 Google 地图上显示了从 A 点到 B 点的路线。这一切都很好。我现在正在做的是通过这样做来缩放以适应地图上的两个标记:

LatLngBounds.Builder builder = new LatLngBounds.Builder();

for (LatLng marker : markerPoints) {
    builder.include(marker);
}

LatLngBounds bounds = builder.build();
int padding = 20; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,
        padding);
map.moveCamera(cu);
map.animateCamera(cu);

问题是这不包括折线,所以如果路线超出屏幕,它会切断折线,如下所示:

关于如何缩放以适应折线的任何想法?谢谢!

【问题讨论】:

    标签: java android google-maps


    【解决方案1】:

    我尝试了这段代码,它对我很有用,我还在这里附加了输出屏幕。

    LatLng from_Latlng=new LatLng(11.9464816,79.8091988);
    LatLng to_Latlong=new LatLng(12.9876327,80.1260089);
    
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    builder.include(from_Latlng);
    builder.include(to_Latlong);
    LatLngBounds bounds = builder.build();
    GoogleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100), 2000, null);
    

    输出画面:-

    【讨论】:

      【解决方案2】:

      我明白了。我只是跟踪我的折线中的每个 LatLng 并将它们添加到 LatLng 构建器

      private class ParserTask extends
              AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {
      
          // Parsing the data in non-ui thread
          @Override
          protected List<List<HashMap<String, String>>> doInBackground(
                  String... jsonData) {
      
              JSONObject jObject;
              List<List<HashMap<String, String>>> routes = null;
      
              try {
                  jObject = new JSONObject(jsonData[0]);
                  DirectionsJSONParser parser = new DirectionsJSONParser();
      
                  // Starts parsing data
                  routes = parser.parse(jObject);
              } catch (Exception e) {
                  e.printStackTrace();
              }
              return routes;
          }
      
          // Executes in UI thread, after the parsing process
          @Override
          protected void onPostExecute(List<List<HashMap<String, String>>> result) {
              ArrayList<LatLng> points = null;
              PolylineOptions lineOptions = null;
              MarkerOptions markerOptions = new MarkerOptions();
              String distance = "";
              String duration = "";
      
              if (result.size() < 1) {
                  Toast.makeText(getBaseContext(), "No Points",
                          Toast.LENGTH_SHORT).show();
                  return;
              }
      
              // Traversing through all the routes
              for (int i = 0; i < result.size(); i++) {
                  points = new ArrayList<LatLng>();
                  lineOptions = new PolylineOptions();
      
                  // Fetching i-th route
                  List<HashMap<String, String>> path = result.get(i);
      
                  // Fetching all the points in i-th route
                  for (int j = 0; j < path.size(); j++) {
                      HashMap<String, String> point = path.get(j);
      
                      if (j == 0) { // Get distance from the list
                          distance = (String) point.get("distance");
                          continue;
                      } else if (j == 1) { // Get duration from the list
                          duration = (String) point.get("duration");
                          continue;
                      }
      
                      double lat = Double.parseDouble(point.get("lat"));
                      double lng = Double.parseDouble(point.get("lng"));
                      LatLng position = new LatLng(lat, lng);
      
                      points.add(position);
                  }
      
                  // Adding all the points in the route to LineOptions
                  lineOptions.addAll(points);
                  lineOptions.width(8);
                  lineOptions.color(Color.BLUE);
      
              }
      
              tvDistanceDuration.setText("Distance:" + distance + ", Duration:"
                      + duration);
      
              // Drawing polyline in the Google Map for the i-th route
              map.addPolyline(lineOptions);
      
              LatLngBounds.Builder builder = new LatLngBounds.Builder();
              /*
               * for (Marker marker : markers) {
               * builder.include(marker.getPosition()); }
               */
              for (LatLng point : points) {
                  builder.include(point);
              }
      
              LatLngBounds bounds = builder.build();
              int padding = 20; // offset from edges of the map in pixels
              CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,
                      padding);
              map.moveCamera(cu);
              map.animateCamera(cu, 2000, null);
          }
      }
      

      【讨论】:

      • 感谢您的示例。但是,您要么移动相机位置,要么设置动画效果。在您的示例中,您已经将相机移动到其位置,因此到达该位置不需要动画移动。如果你真的想为你的 LatLngBounds 设置动画,你应该删除“map.moveCamera(cu”),否则删除 animateCamera 调用,因为它不是必需的。
      【解决方案3】:

      我想说的是,在您构建折线时,您会跟踪线的范围。然后在缩放时使用这些范围而不是点。

      【讨论】:

      • 嗯..这确实有道理。你知道有什么方法可以跟踪这些范围吗?
      • 我得到了它的工作,我考虑了你的建议!谢谢
      • 你能分享你的答案吗@user1282637
      【解决方案4】:

      我是从 Premkumar 那里抄来的。如果你有超过 2 个点并且你想确保一切都合适(也许你有两个点在中间,还有很多东西在周围盘旋)。该解决方案的工作原理是创建一个 minLatLng 和一个 maxLatLng,然后使用 Premkumar 答案中的 newLatLngBounds 解决方案:

      var minLat: Double? = null
      var minLng: Double? = null
      var maxLat: Double? = null
      var maxLng: Double? = null
      allLocs!!.forEach {
          if (minLat == null) {
              minLat = it.latitude
              maxLat = it.latitude
              minLng = it.longitude
              maxLng = it.longitude
          } else {
              minLat = min(it.latitude, minLat!!)
              maxLat = max(it.latitude, maxLat!!)
              minLng = min(it.longitude, minLng!!)
              maxLng = max(it.longitude, maxLng!!)
          }
      }
      val builder = LatLngBounds.builder()
      builder.include(LatLng(minLat!!, minLng!!))
      builder.include(LatLng(maxLat!!, maxLng!!))
      googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100))
      

      显然,如果您需要实时获取此信息,那么每次获得新位置时记录下您的最小值和最大值会更有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-14
        • 1970-01-01
        • 2016-11-04
        • 2013-05-26
        • 1970-01-01
        • 2013-01-27
        相关资源
        最近更新 更多