Search code examples
androidgoogle-mapsgoogle-maps-markersgoogle-maps-api-2

How do I track the location of a user throughout the day in Google Maps?


How would you track the location of a user for the entire day, like the timeline in Google maps?

I have two ideas

  1. For example, if I have 200 LatLng values per day, how do I pass all of these LatLng values to Google map as points? I got one google doc reference in that I can track up to 10 locations points only.

  2. Is there any Google API to track the user throughout the whole day and make a timeline for it?

    enter image description here


Solution

  • 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.