如何在android中绘制可用lat long的路径路径

时间:2017-01-24 12:59:08

标签: android google-maps google-maps-markers

我想在我的移动应用程序中绘制路径路径。一切都工作正常只有路线没有正确,因为这里的道路是我的代码..

 private void formRoadMapUrl(String tempLatLng) {
    StringBuilder urlString = new StringBuilder(ServerConnector.GOOGLE_ROAD_MAP_API + tempLatLng);
    urlString.append("&interpolate=true&key=" + AppConfigurationSetting.GOOGLE_ROAD_MAP_API);
    requestToServer(urlString);
}

这里我将我的lat发送给谷歌..

private void locationTrackPolyline(String output) {
    try {
        JSONObject obj = new JSONObject(output);
        if (obj.has("snappedPoints")) {
            mResultOfGoogleRoadMapAPI++;
            Gson gson = new Gson();
            String result   = obj.getString("snappedPoints");

            Type type = new TypeToken<List<GoogleRoadMap>>(){}.getType();
            List<GoogleRoadMap> batchArrayModel = gson.fromJson(result, type);
            Integer batchListSize = batchArrayModel.size();

            ArrayList<LatLng> polylines = new ArrayList<LatLng>();

            for (GoogleRoadMap batchObj: batchArrayModel) {
                LatLng polyline = new LatLng(Double.valueOf(batchObj.getLocation().getLatitude()),
                        Double.valueOf(batchObj.getLocation().getLongitude()));
                polylines.add(polyline);

                // TODO : animation data passing is google map
                addMarkerToMap(polyline);
            }

            // Polylines are useful for marking paths and routes on the mMap.
            LatLng lat = polylines.get(0);

            Integer getLength = polylines.size();
            LatLng endPoints = polylines.get(getLength - 1);

            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lat, 17));
            if(mResultOfGoogleRoadMapAPI == 1) {
                mMap.addMarker(new MarkerOptions()
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.map_green100))
                        .title("Start Point")
                        .snippet("The most populous city in Australia.")
                        .position(lat));
            }
            mAnimatedPolyline.addAll(polylines);
            if(mIterationOfGoogleRoadMapAPI == mResultOfGoogleRoadMapAPI) {
                mMap.addMarker(new MarkerOptions()
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.map_red100))
                        .title("End Point")
                        .position(endPoints));

                animator.startAnimation(true);
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

在这里,我正在显示我从谷歌获得的路径。

1 个答案:

答案 0 :(得分:1)

//Please add below code 

LatLng mLatLng = new LatLng(23.2156, 72.6569);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(mLatLng, 10);
googlemap.animateCamera(cameraUpdate);


googlemap.addPolyline(new PolylineOptions().add(new LatLng(lat,lng), new LatLng(23.2156, 72.6369)).width(10).color(Color.BLUE).geodesic(true));
MarkerOptions markerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pin));
markerOptions.title(“start");
googlemap.addMarker(markerOptions.position(new LatLng(23.2156, 72.6569)));

MarkerOptions markerOptionsDest = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pin));
markerOptionsDest.title(“end");
googlemap.addMarker(markerOptionsDest.position(new LatLng(23.2156, 72.6369)));