how to get current location in google map android

前端 未结 17 1937
长发绾君心
长发绾君心 2020-11-27 14:04

Actually my problem is I am not getting current location latitude and longitude I tried so many ways.I know that this question already asked in SO I tried that answers also

17条回答
  •  北海茫月
    2020-11-27 14:50

    Add the permissions to the app manifest

    Add one of the following permissions as a child of the element in your Android manifest. Either the coarse location permission:

    
      ...
      
      ...
    
    

    Or the fine location permission:

    
      ...
      
      ...
    
    

    The following code sample checks for permission using the Support library before enabling the My Location layer:

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
        mMap.setMyLocationEnabled(true);
    } else {
        // Show rationale and request permission.
    }
    The following sample handles the result of the permission request by implementing the ActivityCompat.OnRequestPermissionsResultCallback from the Support library:
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == MY_LOCATION_REQUEST_CODE) {
          if (permissions.length == 1 &&
              permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION &&
              grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);
        } else {
          // Permission was denied. Display an error message.
        }
    }
    

    This example provides current location update using GPS provider. Entire Android app code is as follows,

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.widget.TextView;
    
    import android.util.Log;
    
    public class MainActivity extends Activity implements LocationListener{
    protected LocationManager locationManager;
    protected LocationListener locationListener;
    protected Context context;
    TextView txtLat;
    String lat;
    String provider;
    protected String latitude,longitude; 
    protected boolean gps_enabled,network_enabled;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtLat = (TextView) findViewById(R.id.textview1);
    
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    }
    @Override
    public void onLocationChanged(Location location) {
    txtLat = (TextView) findViewById(R.id.textview1);
    txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
    }
    
    @Override
    public void onProviderDisabled(String provider) {
    Log.d("Latitude","disable");
    }
    
    @Override
    public void onProviderEnabled(String provider) {
    Log.d("Latitude","enable");
    }
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d("Latitude","status");
    }
    }
    

提交回复
热议问题