【问题标题】:How do I track the location of a user throughout the day in Google Maps?如何在 Google 地图中全天跟踪用户的位置?
【发布时间】:2018-06-27 05:02:12
【问题描述】:

您将如何跟踪用户一整天的位置,例如 Google 地图中的时间线?

我有两个想法

  1. 例如,如果我每天有 200 个 LatLng 值,我如何将所有这些 LatLng 值作为点传递给 Google 地图?我得到了一个google doc reference,因为我最多只能跟踪 10 个位置点。

  2. 是否有任何 Google API 可以全天跟踪用户并为其制定时间表?

【问题讨论】:

  • 如果您有 200 个 latLngs,那么只需以您想要的任何方式使用它们。在路线的情况下,10 个数字限制是针对目的地的。
  • 你在后台使用什么东西吗?
  • @ヴィシャル 是的,我正在使用 firebase firestore..

标签: android google-maps google-maps-markers google-maps-api-2


【解决方案1】:

如果你有 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 来获取这些点之间的方向,并根据路点请求的结果绘制折线。

【讨论】:

    【解决方案2】:

    我终于找到了解决方案,你可以每 15 分钟得到你想要的任何东西。

    我从 google sample github 获得了参考,我们可以使用 PendingIntent 运行后台服务,也可以使用 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));
                    }
                }
            }
        }
    }
    

    此处完整参考: https://github.com/googlesamples/android-play-location

    部分移动后台服务未运行。如果服务未运行,请按照以下步骤操作:

    在小米设备中,您只需将您的应用添加到自动启动列表,即可 这样做,请按照下面给出的这些简单步骤:

    1.在手机上打开安全应用。

    2.点击权限,它会显示两个选项:自动启动和 权限

    3.点击自动启动,它会显示打开或关闭切换的应用列表 按钮。

    4.打开应用的开关,大功告成!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-02
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 2016-01-13
      相关资源
      最近更新 更多