如何在android中的谷歌静态地图中显示完整路线

时间:2016-12-24 15:56:54

标签: android google-maps

我想在google静态地图上显示两个地方之间的路线,但当两个地方之间的距离图像显示如下: - images show like that

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

请帮帮我。在此先感谢。

1 个答案:

答案 0 :(得分: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);