无法从其他班级获得纬度和经度

时间:2019-01-05 13:03:05

标签: android gps

我有这门课,可以从我的gps中获取经纬度

public class GpsCoord extends AppCompatActivity implements LocationListener {

private LocationManager locationManager;
double longtitude;
double latitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    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
        //    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;
    }
    Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);

    onLocationChanged(location);

}

@Override
public void onLocationChanged(Location location) {

    longtitude=location.getLongitude();
    latitude=location.getLatitude();

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

public double getLongtitude() {
    return longtitude;
}

public double getLatitude() {
    return latitude;
}
}

但是当我创建此类的实例时,尝试使用吸气剂获得纬度和经度,它会给我结果“纬度和经度= 0.0”。在GpsCoord类中,它可以正常工作。静态,仍然出现相同的错误

0 个答案:

没有答案