简体   繁体   English

更新Google地图上的位置

[英]Update location on google maps

I'm wondering what the best/suggested way to update my location on the map is. 我想知道更新地图上位置的最佳/建议方法是什么。 I'm trying to update my location once it changes and draw a line on the map for the route I've walked. 更改位置后,我会尝试更新自己的位置,并在地图上为我走过的路线画一条线。 I was thinking of using a thread who calls for my location method once every 5 sec (?) but I doubt this would be a recomended way to do this. 我当时正在考虑使用一个线程,该线程每5秒调用一次我的定位方法(?),但我怀疑这样做是否值得推荐。

question: Best way to update my location on the map and how do I draw a route for the path I've taken. 问题:更新我在地图上的位置的最佳方法,以及如何为我所走的路径绘制路线。

If anyone could point me to the right direction I would highly appreciate that. 如果有人能指出我正确的方向,我将非常感谢。

You can use this library. 您可以使用库。 It's very useful and customize-able. 这是非常有用且可自定义的。 You can set how frequently you want to get location. 您可以设置获取位置的频率。 Just keep the reference of previous location. 只需保留先前位置的参考即可。 Define global instance of LocationTracker and then set it up like this. 定义LocationTracker全局实例,然后像这样进行设置。

private void setupLocationTracker() {
    TrackerSettings settings = new TrackerSettings()
            .setUseGPS(true)
            .setUseNetwork(true)
            .setUsePassive(true)
            .setTimeBetweenUpdates(1000 * 5)    //You can set it according to your needs
            .setMetersBetweenUpdates(10);    //Or you can set this too.
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    locationTracker = new LocationTracker(this, settings) {
        @Override
        public void onLocationFound(@NonNull Location location) {
            LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
            //if(latLngList.size() == 0) {
            mMap.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 15.0f), 3000, null);
            //Write your function to draw line
            drawLine(previousLocation, currentLocation);
            previousLocation = currentLocation;
        }

        @Override
        public void onTimeout() {
            Log.d("MESSAGE", "Time out");
        }
    };
}

And then call locationTraker.startListening() and locationTracker.stopListening() whenever and wherever you want to. 然后随时随地调用locationTraker.startListening()locationTracker.stopListening() Just don't forget to call stopListening() inside onPause() . 只是不要忘记在onPause()内调用stopListening() onPause()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM