【问题标题】:how I get my location after turning-on-gps打开 GPS 后如何获取我的位置
【发布时间】:2017-03-03 11:00:05
【问题描述】:

我是第一次使用 googleMap,所以请帮助我。我想在打开应用程序时在地图上显示用户位置。当用户已经打开 gps 时,我已经这样做了。

    LocationManager locationManagerCt = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);

    locationCt = locationManagerCt
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
 LatLng latLng=new LatLng(locationCt.getLatitude(), locationCt.getLongitude());
            MarkerOptions marker = new MarkerOptions().position(latLng).title("Current Location").snippet("Discription");

            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));

           // Moving Camera to a Location with animation
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(latLng).zoom(15).build();

            HomeActivity.mGoogleMap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));

            HomeActivity.mGoogleMap.addMarker(marker);

当 gps 关闭时。打开 Gps 后,我尝试做同样的事情。 我从这里打开 gps: https://stackoverflow.com/a/33555732/3393578

用户开启gps后

   @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode == 1000) {
     if(resultCode == Activity.RESULT_OK){
         //after user press ok button on GPS popup 

         /*I've apply same code above but unable to get location
          its give me error on Location (nullpointer) */


     } if (resultCode == Activity.RESULT_CANCELED) {
     } 
} 

}

打开 GPS 后如何在几秒钟内获取位置。 我不想刷新活动。

谢谢!

【问题讨论】:

    标签: android google-maps location


    【解决方案1】:

    首先,切换到FusedLocationApi,而不是现在旧的LocationManager。您需要使用 FusedLocationAPI 的requestLocationUpdates() 方法。

    查看 google 示例以了解详细信息: https://github.com/googlesamples/android-play-location

    【讨论】:

      【解决方案2】:

      请使用这个库,它真的很好用

      https://github.com/mrmans0n/smart-location-lib

      【讨论】:

        【解决方案3】:

        请像这样尝试。

            private FusedLocationProviderClient mFusedLocationClient;
            LocationRequest mLocationRequest;
        
        @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_maps);
                ButterKnife.bind(this);
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
        }
        
        
                public void requestLocationUpdates() {
                        mLocationRequest = new LocationRequest();
                        mLocationRequest.setInterval(120000); // two minute interval
                        mLocationRequest.setFastestInterval(1000);
                        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
                        if (ContextCompat.checkSelfPermission(this,
                                Manifest.permission.ACCESS_FINE_LOCATION)
                                == PackageManager.PERMISSION_GRANTED) {
                            mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
                        }
                    }
        
            @Override
                public void onConnected(Bundle bundle) {
                    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;
                    }
                    requestLocationUpdates();
                    mFusedLocationClient.getLastLocation()
                            .addOnCompleteListener(this, new OnCompleteListener<Location>() {
                                @Override
                                public void onComplete(@NonNull Task<Location> task) {
                                    if (task.isSuccessful() && task.getResult() != null) {
                                        mLastLocation = task.getResult();
        
                                    } else {
        
        
                                        Log.w(TAG, "getLastLocation:exception", task.getException());
                                        Toast.makeText(mContext, "No Location Detected.", Toast.LENGTH_LONG).show();
                                    }
                                }
                            });
        
        
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-05-06
          • 1970-01-01
          • 2018-10-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多