如何在 Google 地图中全天跟踪用户的位置?
How do I track the location of a user throughout the day in Google Maps?
您将如何跟踪用户一整天的位置,例如 Google 地图中的时间轴?
我有两个想法
例如,如果我每天有 200 个 LatLng
值,我如何将所有这些 LatLng
值作为点传递到 Google 地图?我有一个 google doc reference,因为我最多只能跟踪 10 个位置点。
有没有GoogleAPI可以全天跟踪用户并做一个时间线的?
如果你有 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 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 part of Directions API 获取这些点之间的方向,并根据 waypoints 请求的结果绘制折线。
我终于找到了解决方案,您可以每 15 分钟获取一次 latlng,随心所欲。
我从 google 示例 github 中获得参考,我们可以 运行 使用 PendingIntent 的后台服务,或者我们可以使用广播接收器。
public class LocationUpdatesIntentService extends IntentService {
private static final String ACTION_PROCESS_UPDATES =
"com.google.android.gms.location.sample.locationupdatespendingintent.action" +
".PROCESS_UPDATES";
private static final String TAG = LocationUpdatesIntentService.class.getSimpleName();
public LocationUpdatesIntentService() {
// Name the worker thread.
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_PROCESS_UPDATES.equals(action)) {
LocationResult result = LocationResult.extractResult(intent);
if (result != null) {
List<Location> locations = result.getLocations();
Utils.setLocationUpdatesResult(this, locations);
Utils.sendNotification(this, Utils.getLocationResultTitle(this, locations));
Log.i(TAG, Utils.getLocationUpdatesResult(this));
}
}
}
}
}
这里完整参考:
https://github.com/googlesamples/android-play-location
某些移动后台服务未 运行ning..如果服务未 运行ning 请按照以下步骤操作:
In Xiaomi devices, you just have to add your app to Autostart list, to
do so, follow these simple steps given below:
1.Open Security app on your phone.
2.Tap on Permissions, it'll show you two options: Autostart and
Permissions
3.Tap on Autostart, it'll show you list of apps with on or off toggle
buttons.
4.Turn on toggle of your app, you're done!
您将如何跟踪用户一整天的位置,例如 Google 地图中的时间轴?
我有两个想法
例如,如果我每天有 200 个
LatLng
值,我如何将所有这些LatLng
值作为点传递到 Google 地图?我有一个 google doc reference,因为我最多只能跟踪 10 个位置点。有没有GoogleAPI可以全天跟踪用户并做一个时间线的?
如果你有 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 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 part of Directions API 获取这些点之间的方向,并根据 waypoints 请求的结果绘制折线。
我终于找到了解决方案,您可以每 15 分钟获取一次 latlng,随心所欲。
我从 google 示例 github 中获得参考,我们可以 运行 使用 PendingIntent 的后台服务,或者我们可以使用广播接收器。
public class LocationUpdatesIntentService extends IntentService {
private static final String ACTION_PROCESS_UPDATES =
"com.google.android.gms.location.sample.locationupdatespendingintent.action" +
".PROCESS_UPDATES";
private static final String TAG = LocationUpdatesIntentService.class.getSimpleName();
public LocationUpdatesIntentService() {
// Name the worker thread.
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_PROCESS_UPDATES.equals(action)) {
LocationResult result = LocationResult.extractResult(intent);
if (result != null) {
List<Location> locations = result.getLocations();
Utils.setLocationUpdatesResult(this, locations);
Utils.sendNotification(this, Utils.getLocationResultTitle(this, locations));
Log.i(TAG, Utils.getLocationUpdatesResult(this));
}
}
}
}
}
这里完整参考: https://github.com/googlesamples/android-play-location
某些移动后台服务未 运行ning..如果服务未 运行ning 请按照以下步骤操作:
In Xiaomi devices, you just have to add your app to Autostart list, to do so, follow these simple steps given below:
1.Open Security app on your phone.
2.Tap on Permissions, it'll show you two options: Autostart and Permissions
3.Tap on Autostart, it'll show you list of apps with on or off toggle buttons.
4.Turn on toggle of your app, you're done!