如何全天在Google地图中跟踪用户的位置? [英] How do I track the location of a user throughout the day in Google Maps?

查看:109
本文介绍了如何全天在Google地图中跟踪用户的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何跟踪用户在一整天的位置,例如Google地图中的时间轴?

How would you track the location of a user for the entire day, like the timeline in Google maps?

我有两个主意

  1. 例如,如果我每天有200个LatLng值,如何将所有这些LatLng值作为点传递给Google地图?我得到了一个 Google文档参考,因为我最多可以跟踪10个位置点

  1. For example, if I have 200 LatLng values per day, how do I pass all of these LatLng values to Google map as points? I got one google doc reference in that I can track up to 10 locations points only.

是否有Google API可以全天跟踪用户并为其设置时间表?

Is there any Google API to track the user throughout the whole day and make a timeline for it?

推荐答案

如果您有200个LatLng点,则始终可以将其绘制为

If you have 200 LatLng point you always can just draw them as polyline:

...
final List<LatLng> polylinePoints = new ArrayList<>();
polylinePoints.add(new LatLng(<Point1_Lat>, <Point1_Lng>));
polylinePoints.add(new LatLng(<Point2_Lat>, <Point2_Lng>));
...

final Polyline polyline = mGoogleMap.addPolyline(new PolylineOptions()
        .addAll(polylinePoints)
        .color(Color.BLUE)
        .width(20));

,并根据需要使用贴紧道路将它们贴紧道路> Google Maps Roads API 的一部分:

and, if you need, snap them to roads with Snap to Road part of Google Maps Roads API:

...
List<LatLng> snappedPoints = new ArrayList<>();
new GetSnappedPointsAsyncTask().execute(polylinePoints, null, snappedPoints);
...

private class GetSnappedPointsAsyncTask extends AsyncTask<List<LatLng>, Void, List<LatLng>> {

    protected void onPreExecute() {
        super.onPreExecute();
    }

    protected List<LatLng> doInBackground(List<LatLng>... params) {

        List<LatLng> snappedPoints = new ArrayList<>();

        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(buildRequestUrl(params[0]));
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();

            InputStream stream = connection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuilder jsonStringBuilder = new StringBuilder();

            StringBuffer buffer = new StringBuffer();
            String line = "";

            while ((line = reader.readLine()) != null) {
                buffer.append(line+"\n");
                jsonStringBuilder.append(line);
                jsonStringBuilder.append("\n");
            }

            JSONObject jsonObject = new JSONObject(jsonStringBuilder.toString());
            JSONArray snappedPointsArr = jsonObject.getJSONArray("snappedPoints");

            for (int i = 0; i < snappedPointsArr.length(); i++) {
                JSONObject snappedPointLocation = ((JSONObject) (snappedPointsArr.get(i))).getJSONObject("location");
                double lattitude = snappedPointLocation.getDouble("latitude");
                double longitude = snappedPointLocation.getDouble("longitude");
                snappedPoints.add(new LatLng(lattitude, longitude));
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return snappedPoints;
    }

    @Override
    protected void onPostExecute(List<LatLng> result) {
        super.onPostExecute(result);

        PolylineOptions polyLineOptions = new PolylineOptions();
        polyLineOptions.addAll(result);
        polyLineOptions.width(5);
        polyLineOptions.color(Color.RED);
        mGoogleMap.addPolyline(polyLineOptions);

        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        builder.include(result.get(0));
        builder.include(result.get(result.size()-1));
        LatLngBounds bounds = builder.build();
        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));

    }
}


private String buildRequestUrl(List<LatLng> trackPoints) {
    StringBuilder url = new StringBuilder();
    url.append("https://roads.googleapis.com/v1/snapToRoads?path=");

    for (LatLng trackPoint : trackPoints) {
        url.append(String.format("%8.5f", trackPoint.latitude));
        url.append(",");
        url.append(String.format("%8.5f", trackPoint.longitude));
        url.append("|");
    }
    url.delete(url.length() - 1, url.length());
    url.append("&interpolate=true");
    url.append(String.format("&key=%s", <your_Google_Maps_API_key>);

    return url.toString();
}

如果相邻点之间的距离太大,则可以使用 Waypoints Directions API 的一部分,以获取该点之间的方向并绘制带有航路点请求结果的折线.

If distance between adjacent points too big, you can use Waypoints part of Directions API to get directions between that points and draw polyline with results of waypoints request.

这篇关于如何全天在Google地图中跟踪用户的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆