运行地图时显示当前位置

时间:2017-03-30 03:21:56

标签: java android google-maps

如何在应用运行时获取当前位置。我知道setMyLocationEnabled(true),我在stackoverflow上看到很多问题,但没有得到完美答案。

我试试这段代码

 private GoogleMap mMap;
    LocationManager locationManager;
    Location location;
    String provider;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

//==================================================================================================
        //get user location code
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        provider = locationManager.getBestProvider(new Criteria(), false);
        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) {
            // TODO: Consider calling
            return;
        }
        location = locationManager.getLastKnownLocation(provider);

        if(location != null){
            Toast.makeText(this,"Location get"+location.getLatitude()+" --- "+location.getLatitude(),Toast.LENGTH_SHORT).show();
        }else{
            if(location == null){
                Toast.makeText(this,"Location Not get",Toast.LENGTH_SHORT).show();
            }

        }
//==================================================================================================
    }

//==================================================================================================
    // map code
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        if (location != null) {
            //here where the camera animate and zoom to particular location.
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(location.getLatitude(), location.getLongitude()), 14));

            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(location.getLatitude(), location.getLongitude()))      // Sets the center of the map to location user
                    .zoom(15)                   // Sets the zoom
                    .bearing(90)                // Sets the orientation of the camera to east// Sets the tilt of the camera to 30 degrees
                    .build();                   // Creates a CameraPosition from the builder
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        }
                                                   // Add a marker in Sydney and move the camera
        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) {
            // TODO: Consider calling
            return;
        }
        mMap.setMyLocationEnabled(true);

//==================================================================================================
        }@Override
    protected void onResume() {
        super.onResume();
        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) {
            // TODO: Consider calling
            return;
        }
        locationManager.requestLocationUpdates(provider, 4000, 1, this);
    }
    @Override
    protected void onPause() {
        super.onPause();
        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) {
            // TODO: Consider calling
            return;
        }
        locationManager.removeUpdates(this);
    }
//==================================================================================================
    @Override
    public void onLocationChanged(Location location) {
  Toast.makeText(this,"Location Changed",Toast.LENGTH_LONG).show();
}
    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {
        Toast.makeText(this,"onStatus...",Toast.LENGTH_LONG).show();
    }
    @Override
    public void onProviderEnabled(String s) {
        Toast.makeText(this,"onProvider",Toast.LENGTH_LONG).show();
    }
    @Override
    public void onProviderDisabled(String s) {
        Toast.makeText(this,"onProviderDis",Toast.LENGTH_LONG).show();

}

0 个答案:

没有答案
相关问题