【问题标题】:Android Google Maps onLocationChanged not calledAndroid Google Maps onLocationChanged 未调用
【发布时间】:2018-03-16 03:24:44
【问题描述】:

我知道有很多关于这个问题的线索,但没有什么能给我很好的答案。永远不会调用函数 onLocationChanged 接缝。

 @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    mMap.setMyLocationEnabled(true);
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);

    LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Toast.makeText(getApplicationContext(), "New Location", Toast.LENGTH_SHORT).show();
            LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
            mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(20));
            mMap.addMarker(new MarkerOptions().position(latLng).title("Jesteś tutaj!"));
        }

        /* ... */
    };
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 2, locationListener);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 2, locationListener);
}

我做错了什么?

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    首先,您需要在真实设备而非虚拟设备中运行您的应用,并通过设置保持 GPS 开启。然后,您可以使用以下为我完成工作的方法。

        private void createMap()
        {
            TotalDistance.setText("Distance:");
            SupportMapFragment supportMapFragment=(SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
            //SupportMapFragment fm = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
            googleMap=supportMapFragment.getMap();
    
     /*       googleMap = ((SupportMapFragment) MainActivity.fragmentManager
                    .findFragmentById(R.id.map)).getMap();*/
            locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
            // Creating a criteria object to retrieve provider
            Criteria criteria = new Criteria();
    
            // Getting the name of the best provider
            provider = locationManager.getBestProvider(criteria, true);
    
            // Getting Current Location
            if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.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;
            }
    
            location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
    
            googleMap.getUiSettings().setRotateGesturesEnabled(false);
    
            marker = new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title(location.getLatitude() + " " + location.getLongitude());
            googleMap.addMarker(marker);
            CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(15).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    
            googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
            origin_latLng = new LatLng(location.getLatitude(), location.getLongitude());
        }
    
        private void intializeMap(Context context) {
            int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
    
            // Showing status
            if (status != ConnectionResult.SUCCESS) { // Google Play Services are not available
    
                int requestCode = 10;
                Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, getActivity(), requestCode);
                dialog.show();
    
            } else { // Google Play Services are available
    
                // Getting reference to the SupportMapFragment of activity_main.xml
    
                if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), android.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;
                }
                googleMap.setMyLocationEnabled(true);
                googleMap.clear();
    
                if (location != null) {
                    onLocationChanged(location);
                }
    
                locationManager.requestLocationUpdates(provider, 2000, 1, YOUR ACTIVITY REFERENCE);
                googleMap.stopAnimation();
    
            }
        }
    
        @Override
        public void onLocationChanged(Location location) {
    
            // Getting latitude of the current location
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            finalLatlang=new LatLng(latitude,longitude);
            //  userlatLang.add(finalLatlang);
    
            CameraPosition cameraPosition = new CameraPosition.Builder().target(finalLatlang).zoom(15).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            Log.d("Latitude", " " + latitude);
            Log.d("Longitude", " " + longitude);
    
            // Showing the current location in Google Map
            googleMap.moveCamera(CameraUpdateFactory.newLatLng(finalLatlang));
    
            if(flag==1) {
    
                // Zoom in the Google Map
                googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
                flag++;
            }
    
        }
    

    还请在您的 Activity 或 Fragment 中实现 LocationListener

    【讨论】:

      【解决方案2】:
       Criteria crit = new Criteria();
       crit.setAccuracy(Criteria.ACCURACY_FINE);
       String best = mgr.getBestProvider(crit, false);
      

      以上是为了获得最好的位置提供者。

      你可以在 onResume() 中给出你的 locationManager 请求;

       @Override public void onResume() {
      super.onResume();
      locationManager.requestLocationUpdates(, 10000, 1, locationListener);}
      

      希望对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-19
        • 2023-03-13
        • 2016-12-08
        • 1970-01-01
        • 1970-01-01
        • 2012-08-30
        • 1970-01-01
        • 2017-08-09
        相关资源
        最近更新 更多