如何使用谷歌地图跟踪用户全天位置? [英] How to track users full day location using google map?
问题描述
我想要建议如何跟踪谷歌地图中时间线整日的用户位置。
我有两个想法
1.例如,如果我每天有200 latlng如何在Google地图中传递整天的latlng值。分
我有一个
如果您有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>));
...
最终Polyline polyline = mGoogleMap.addPolyline(新PolylineOptions()
.addAll(polylinePoints)
.color(Color.BLUE)
.width(20));
,如果需要的话,用 Snap to Road 部分
...
List< rel =nofollow noreferrer> Google Maps Roads API ;经纬度> snappedPoints = new ArrayList<>();
new GetSnappedPointsAsyncTask()。execute(polylinePoints,null,snappedPoints);
...
private class GetSnappedPointsAsyncTask extends AsyncTask< List< LatLng>,Void,List< LatLng>> {
保护无效onPreExecute(){
super.onPreExecute();
}
保护列表< LatLng> doInBackground(List< LatLng> ... params){
List< LatLng> snappedPoints = new ArrayList<>();
HttpURLConnection连接= null;
BufferedReader reader = null;
尝试{
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 =; ((line = reader.readLine())!= null){
buffer.append(line +\\\
);
jsonStringBuilder.append(line);
jsonStringBuilder.append(\\\
);
}
JSONObject jsonObject = new JSONObject(jsonStringBuilder.toString());
JSONArray snappedPointsArr = jsonObject.getJSONArray(snappedPoints); (); getBSONObject(());
for(int i = 0; i< snappedPointsArr.length(); i ++){
JSONObject snappedPointLocation =((JSONObject)(snappedPointsArr.get(i)))。getJSONObject 位置);
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();
}
尝试{
if(reader!= null){
reader.close();
}
} catch(IOException e){
e.printStackTrace();
}
}
return snappedPoints;
}
@Override
保护无效onPostExecute(List< LatLng>结果){
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=); (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();
}
如果相邻点之间的距离太大,可以使用 Waypoint 的一部分路线API 以获取该点之间的路线,并绘制折线和航点请求的结果。
I want suggestion How to track location of user for entire day like timeline in Google maps.
I have two ideas
1.For example If i have 200 latlng per day how to pass whole day latlng values in google map. points I got one google doc reference in that i can track upto 10 locations points only
2.Idea two : Is there any google api to track user entire day & make a timeline for it
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.
这篇关于如何使用谷歌地图跟踪用户全天位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!