onLocationChanged()无法正常工作

时间:2015-11-29 06:11:07

标签: java android gps location

我正在尝试跟踪用户位置并根据他的移动在地图上绘制路径/路线(updatePolyline(),updateCamera(),updateMarker()负责绘制路线)。程序编译,但关键的错误是当位置实际改变时不调用onLocationChanged(),因此,没有路径被绘制。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {

        private GoogleMap mMap; // Might be null if Google Play services APK is not available.
        private PolylineOptions mPolylineOptions;
        LocationManager locationManager;
        private LatLng mLatLng;

        double latitude, longitude;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
            setUpMapIfNeeded();
          //  LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            this.locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            longitude = location.getLongitude();
            latitude = location.getLatitude();
    //        if(location != null) {
    //
    //            onLocationChanged(location);
    //        }
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, this);
        }



        public void onLocationChanged(Location location) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();
            mLatLng = new LatLng(latitude, longitude);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    updatePolyline();
                    updateCamera();
                    updateMarker();
                }
            });
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }

        @Override
        public void onMapReady(GoogleMap map) {
            mMap = map;
            initializeMap();
        }

        private void updatePolyline() {
            mMap.clear();
            mMap.addPolyline(mPolylineOptions.add(mLatLng));
        }

        private void updateMarker() {
            mMap.addMarker(new MarkerOptions().position(mLatLng));
        }

        private void updateCamera() {
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mLatLng, 16));
        }

        private void initializeMap() {
            mPolylineOptions = new PolylineOptions();
            mPolylineOptions.color(Color.BLUE).width(10);
        }

        @Override
        protected void onResume() {
            super.onResume();
            setUpMapIfNeeded();
        }

        private void setUpMapIfNeeded() {
            if (mMap == null) {
                mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
                mMap.setMyLocationEnabled(true);
                mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
                    @Override
                    public boolean onMyLocationButtonClick() {
                        LocationManager lm = null;
                        boolean gps_enabled = false, network_enabled = false;
                        if (lm == null)
                            lm = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
                        try {
                            gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
                        } catch (Exception ex) {
                        }
                        try {
                            network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                        } catch (Exception ex) {
                        }
                        if (!gps_enabled && !network_enabled) {
                            AlertDialog.Builder dialog = new AlertDialog.Builder(MapsActivity.this);
                            dialog
                                    .setTitle("No gps")
                                    .setPositiveButton("Atšaukti", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface paramDialogInterface, int paramInt) {

                                        }
                                    })
                                    .setNegativeButton("Open settings", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                                            Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                            MapsActivity.this.startActivity(myIntent);
                                        }
                                    });
                            AlertDialog alert_dialog = dialog.create();
                            alert_dialog.show();

                        }
                        return false;
                    }
                });
            }
        }
    }

0 个答案:

没有答案