问题
How would you track the location of a user for the entire day, like the timeline in Google maps?
I have two ideas
For example, if I have 200
LatLng
values per day, how do I pass all of theseLatLng
values to Google map as points? I got one google doc reference in that I can track up to 10 locations points only.Is there any Google API to track the user throughout the whole day and make a timeline for it?
回答1:
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));
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();
}
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.
回答2:
Finally I got solution for this, You can able to get latlng every 15 mins what every you want.
I got reference from google sample github we can run background service using PendingIntent or we can use Broadcast Receiver.
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));
}
}
}
}
}
Chere here complete reference: https://github.com/googlesamples/android-play-location
Some of the mobile Background Service is not running..if service is not running follow below steps:
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!
来源:https://stackoverflow.com/questions/48315946/how-do-i-track-the-location-of-a-user-throughout-the-day-in-google-maps