android - 如何在 Google map 中全天跟踪用户的位置?

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

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

我有两个想法

  1. 例如,如果我每天有 200 个 LatLng 值,我如何将所有这些 LatLng 值作为点传递给 Google map ?我有一个 google doc reference因为我最多只能跟踪 10 个位置点。

  2. 是否有任何 Google API 可以全天跟踪用户并为其制定时间表?

    enter image description here

最佳答案

如果您有 200 个 LatLng 点,您总是可以将它们绘制为 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));

并且,如果需要,使用 Snap to Road 将它们捕捉到道路上的一部分 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获取这些点之间的方向并根据航路点请求的结果绘制多段线。

关于android - 如何在 Google map 中全天跟踪用户的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48315946/

相关文章:

android - 如何在android中用不透明度填充矩形

android - 我应该使用 'hardware' 将 NFC 数据写入空标签吗?

java - 使用摇一摇功能打开不同的 Activity

Javascript/jQuery - 浮点验证?

javascript - 显示标记的信息窗口时出错

javascript - Google map api v3 多个标记 - 滚动条和信息窗口的问题

java - 在不使用脚本引擎的情况下评估 kotlin 中的字符串表达式

javascript - 删除 Google Maps API V3 中的平移控件

android - 检查 GPS 位置是否在 android 中另一个 GPS 位置的一定半径内

debugging - Google Maps SVG 图像标记图标未在 IE11 中显示