Java Google地图API无法获取当前的纬度和经度

Java Google地图API无法获取当前的纬度和经度,java,android,Java,Android,我试图得到坐标,但没有成功。结果是0。但当我写作时: googleMap.setMyLocationEnabled(true); 我能看到蓝点。我试图通过以下代码获取这些值: lat = googleMap.getMyLocation().getLatitude(); lng = googleMap.getMyLocation().getLongitude(); 之后,lat和lng为0 我的全部代码: public class GPS_maps extends Activity {

我试图得到坐标,但没有成功。结果是0。但当我写作时:

googleMap.setMyLocationEnabled(true);
我能看到蓝点。我试图通过以下代码获取这些值:

lat = googleMap.getMyLocation().getLatitude();
lng = googleMap.getMyLocation().getLongitude();
之后,lat和lng为0

我的全部代码:

public class GPS_maps extends Activity {
    // Google Map
    private GoogleMap googleMap;
    static Location currentLocation;
    static double lat , lng;
    TextView coordinates;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        coordinates = (TextView) findViewById(R.id.txt_coordinates);
        try {
            // Loading map
            initilizeMap();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initilizeMap() {
        if (googleMap == null) {            
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
            // googleMap.setMyLocationEnabled(true);
            lat = googleMap.getMyLocation().getLatitude();
            lng = googleMap.getMyLocation().getLongitude();
            coordinates.setText(String.valueOf(lat) +" , " +String.valueOf(lng));
            LatLng coordinate = new LatLng(lat, lng);
            CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
            googleMap.animateCamera(yourLocation);
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                    "Unable to create maps", Toast.LENGTH_SHORT)
                    .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
       initilizeMap();
    }}
}
我没有发现任何错误。我的错误在哪里?

尝试以下方法:

从lat、lan声明中删除静态修饰符。 确保以正确的语法发送到setCoordiante。
为确保lat和lan正确,请在获取后立即打印出来。

地图可能没有您在该时间点的位置。尝试将onMyLocationChangeListener添加到地图中,并在第一次调用时设置您的位置。删除了静态设置,但没有区别。0在获得变量后立即打印出它们。上面写了什么?同时打印出googleMap对象,确保它像你想要的那样存在。你说打印googleMap对象是什么意思?我是否在获得VAR后打印VAR?你在获得后有什么需要?哪里?System.out.printllat-长时间执行相同操作