gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:56:24 28 4
gpt4 key购买 nike

您将如何跟踪用户一整天的位置,例如 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com