【问题标题】:Draw path along the Lat and Long values沿 Lat 和 Long 值绘制路径
【发布时间】:2016-03-14 11:41:31
【问题描述】:

大家好,我正在研究 Google 地图的实施。

我正在尝试通过谷歌地图绘制路径。我有按钮来启动和停止 GPS,基本上是为了获取纬度和经度值。

例如:我可能会得到 10 个纬度和经度值并将它们存储在 ArrayList 中。

现在我正在尝试使用上面的 lat 和 long 列表绘制路径,如果我使用 Google maps API,那么我只需要发送 ArrayList 的起点和终点,然后它在地图上绘制/渲染,但它不是我想要的方式。

我想沿着 lat 和 long 值(10 个值)列表绘制路径。原因是谷歌只是给了我们从头到尾的路径,但它可能不是用户旅行的方式/路线.

我们怎样才能做到这一点?

以下是我正在做的事情:

private String getMapsApiDirectionsUrl(LatLng source, LatLng destination, String mode) {
        // Origin of route
        String str_origin = "origin=" + source.latitude + "," + source.longitude;
        // Destination of route
        String str_dest = "destination=" + destination.latitude + "," + destination.longitude;
        // Sensor enabled
        String sensor = "sensor=false";
        // Building the parameters to the web service
        String parameters = str_origin + "&" + str_dest + "&" + sensor + "&" + mode + "&alternatives=true";
        // Output format
        String output = "json";
        // Building the url to the web service
        String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;

        Log.e(TAG, " " + url);
        return url;
    }

private class ReadTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... url) {
            String data = "";
            try {
                HttpConnection http = new HttpConnection();
                data = http.readUrl(url[0]);
            } catch (Exception e) {
                Log.d("Background Task", e.toString());
            }
            return data;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            new ParserTask().execute(result);
        }
    }

    private class ParserTask extends
            AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {

        @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]);
                PathJSONParser parser = new PathJSONParser();
                routes = parser.parse(jObject);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return routes;
        }

        @Override
        protected void onPostExecute(List<List<HashMap<String, String>>> routes) {
            task.callBack(routes);
        }
    }

编辑

我正在按照here 显示的方式进行操作,但发生的情况是基于开始和结束值谷歌地图 APi V2 为我提供了路线,但这可能不是用户已经走过的路线,因为他可能在开始和结束之间访问了一些地方。

【问题讨论】:

  • @jaydroider 是的,我正在按照它在那里显示的方式进行操作,但是会发生什么基于开始和结束值谷歌地图 APi V2 给了我路线,但这可能不是用户拥有的路线旅行,因为他可能在开始和结束之间访问了一些地方。
  • 我认为您需要创建服务,该服务将在某个时间间隔内获取用户的位置,并通过将该位置每隔一段时间传递给 Google API (maps.googleapis.com/maps/api/directions/…),您将在用户旅行时绘制路径。跨度>

标签: android google-maps google-maps-android-api-2


【解决方案1】:

好的,我从您的问题中得到的是,您有 10 个 latlong,并且您想在单条路线中绘制它们。这是解决方案-

  • 创建 latlong 的数组列表
  • 将 latlong 值分配到数组列表中
  • 将此数组列表添加到折线选项
  • 在地图上画出来

以编程方式

ArrayList<LatLng> points = new ArrayList<LatLng>();
PolylineOptions lineOptions = new PolylineOptions()
points.add(position); //positions are the latlng;
lineOptions.addAll(points);
            lineOptions.width(8);
            lineOptions.color(Color.RED);
googleMap.addPolyline(lineOptions); //googleMap is the googlemap object which you are displaying on activity.

祝你好运。

如果这是你想要的,请告诉我。

【讨论】:

  • 谢谢,但它添加了直线,而且线条也偏离了道路。所以如果我使用 Map API,它会添加曲线,但它会根据起点和终点绘制路径,而不是所有 10点。
  • 请先在代码中执行此操作。这永远不会画一条直线。该行将从您提供的第一个 latlng 开始,并在最后一个 latlng 结束。这也将通过您给出的所有分数。直线将从一个 latlng 绘制到下一个 latlng,而不是从源到目的地。如果您需要更精确的路线,那么您需要沿路线更多的点。
  • 我试过了,路径偏离路线,我的意思是偏离道路/行车路线。
  • 是的,这不是强制性的路径将在路线上。因为你的路径是根据你提供的 latlng 绘制的,它不知道路径在哪里。
  • 如果您想获得道路上的路径并且该路径将通过您的特定纬度,那么您必须重新发送查询到方向 api 并且您提到的那些点必须在道路上或靠近路。
【解决方案2】:

这是我正在使用折线在 googlemap 中绘制路径的方法

  LatLng prev = new LatLng(pLati, plongi);
    LatLng my = new LatLng(latid, longid);

    Polyline line = googlemap.addPolyline(new PolylineOptions()
            .add(prev, my).width(5).color(Color.RED));

【讨论】:

  • 我也在做同样的事情,但我正在尝试使用所有 10 个纬度值绘制折线,以便在地图上获得准确的路线
  • 你必须为此使用forloop
【解决方案3】:

您可以按照以下解决方案进行。

PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int i = 0; i < list.size(); i++) {
    LatLng point = list.get(i);
    options.add(point);
}

Polyline line = myMap.addPolyline(options);

希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 2019-05-02
    • 2011-04-09
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多