将我当前的位置保存在手机中

时间:2016-06-09 09:16:05

标签: android google-maps android-gps

我希望在地图中保存我的位置(纬度,经度),当我在genymotion模拟器中测试此代码时,但是在我的手机中,尽管已激活GPS,应用程序仍然崩溃。

 if (googleMap == null) {

            googleMap = ((MapFragment) getFragmentManager().
                  findFragmentById(R.id.map)).getMap();

        }
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        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) {


        }

        googleMap.setMyLocationEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);
        googleMap.getUiSettings().setCompassEnabled(true);
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        googleMap.getUiSettings().setAllGesturesEnabled(true);
        googleMap.setTrafficEnabled(true);

        LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);

        Button curent = (Button)findViewById(R.id.curent_button);
        curent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                LocationManager locationManager1 = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
                if (locationManager1.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

                    LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

     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) {

                        return;
                    }
 Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  System.out.println("lat" + location.getLatitude() + "lng" + location.getLongitude());

       }
        });

0 个答案:

没有答案