【问题标题】:Android Google maps draw path while movingAndroid 谷歌地图在移动时绘制路径
【发布时间】:2016-12-26 11:08:07
【问题描述】:

您好,我正在使用带有LocationListener 的谷歌地图。我可以使用 Fused API

在点之间绘制路径
    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000 * 60 * 1);
        mLocationRequest.setFastestInterval(1000 * 60 * 1);
                                            }
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

我在这里画了路径:

    public void onLocationChanged(Location location) {
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(latLng);
                routePoints.add(latLng);
                Polyline route = mGoogleMap.addPolyline(new PolylineOptions()
                        .width(5)
                        .color(Color.BLUE)
                        .geodesic(false)
                        .zIndex(3));
                route.setPoints(routePoints);

                                            }

关键是,无论LocationRequest的间隔时间如何,我都需要在用户移动时绘制实时路径,并在用户停止时停止。

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    试试这个方法,这样您就可以根据需要决定和更改值。调用这个方法需要多少时间间隔和多少最小距离。也可以在没有互联网的情况下使用。

        private LocationManager locationManager;
        private android.location.LocationListener myLocationListener;
    
        public void checkLocation() {
    
            String serviceString = Context.LOCATION_SERVICE;
            locationManager = (LocationManager) getSystemService(serviceString);
    
    
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
    
    
            myLocationListener = new android.location.LocationListener() {
                public void onLocationChanged(Location locationListener) {
    
                    if (isGPSEnabled(YourActivityName.this)) {
                        if (locationListener != null) {
                            if (ActivityCompat.checkSelfPermission(YourActivityName.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(YourActivityName.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                                return;
                            }
    
                            if (locationManager != null) {
                                location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                                if (location != null) {
                                    latitude = location.getLatitude();
                                    longitude = location.getLongitude();
                                }
                            }
                        }
                    } else if (isInternetConnected(YourActivityName.this)) {
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
    
    
    
                }
    
                public void onProviderDisabled(String provider) {
    
                }
    
                public void onProviderEnabled(String provider) {
    
                }
    
                public void onStatusChanged(String provider, int status, Bundle extras) {
    
                }
            }; 
    
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, myLocationListener);  //  here the min time interval and min distance 
    }
    

    isInternetConnected 方法

    public static boolean isInternetConnected(Context ctx) {
            ConnectivityManager connectivityMgr = (ConnectivityManager) ctx
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo wifi = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            // Check if wifi or mobile network is available or not. If any of them is
            // available or connected then it will return true, otherwise false;
            if (wifi != null) {
                if (wifi.isConnected()) {
                    return true;
                }
            }
            if (mobile != null) {
                if (mobile.isConnected()) {
                    return true;
                }
            }
            return false;
        }
    

    isGpsEnabled 方法

      public boolean isGPSEnabled(Context mContext) {
            LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
            return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        }
    

    逻辑:通过保留变量检查lastlocation 是否为currantLocation,如果是,则表示如果没有绘制路径,则不会移动

    【讨论】:

    • 谢谢,但我想要一种没有时间间隔的方法,我的意思是在用户移动时画线而不是在间隔之后,由于电池问题,我无法将间隔设为非常小的间隔。我想要一个仅在用户移动时才起作用的程序。
    • @Radwa 请注意locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, myLocationListener); 这并不意味着它每隔一秒就会被调用一次,这意味着差异需要至少 1 秒和 1 米才能再次调用该方法。如果位置不会再改变它永远不会被调用更多
    • 我明白你的想法,我的问题是我间隔 1 分钟绘制路径,但忽略了我在不到一分钟内检查的点,即如果我在 A 检查,然后转向 B,然后从 C 绘制点 A 到点 C 忽略点 b,因为它需要不到一分钟
    • @Radwa 这是因为你绘制路径的方式是错误的。你正在添加醋栗位置并画一条线。它应该是从最后一个位置到你的醋栗位置。所以每次你调用它时,它都会从以前的位置绘制一个到当前位置
    • @Radwa 检查我使用的逻辑 -->stackoverflow.com/a/41294229/5188159
    【解决方案2】:

    在你的课堂上定义这个 google api 方法:

    private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
         // make your code to draw path , and refresh the map to not hve a ot of path .
    
         }
    

    更多详情请看here

    【讨论】:

    • 谢谢,但它已被弃用,而且它也需要时间间隔
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多