【问题标题】:How to show full route in google static map in android如何在android的谷歌静态地图中显示完整路线
【发布时间】:2016-12-25 01:45:54
【问题描述】:

我试图在谷歌静态地图上显示两个地方之间的路线,但是当两个地方之间的距离图像显示如下:-

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

请帮助我。提前致谢。

【问题讨论】:

  • 您可以使用可见参数定义静态地图的视口。更多详细信息请参阅文档:developers.google.com/maps/documentation/static-maps/…
  • 我遇到了同样的问题,这就是我所做的: 1. 我添加了 lats,然后将 longs 平均得到中点。 2. 然后我计算了两点之间的距离 3. 然后我使用 trail 方法根据之间的距离创建最佳缩放级别。所以我有一个 switch 语句来根据点之间的距离选择缩放级别。然后我在平均点上应用相机变焦
  • @PeterMushirih 你能给我一些例子或解释一下吗?

标签: android google-maps


【解决方案1】:

我假设您正在使用这样的折线绘制路径

所以我添加了计算距离的函数

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

【讨论】:

    猜你喜欢
    • 2012-07-28
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多