0

現在の座標を取得したいので、次に試します

GoogleMap gm=getMap();
gm.setMyLocationEnabled(true);
Location location=getMap().getMyLocation();

しかし、getMap() は null を返します。

Criteria criteria = new Criteria();
String provider = service.getBestProvider(criteria, false);
Location location = service.getLastKnownLocation(provider);
if (location.getLatitude()!=0){
LatLng userLocation = new LatLng(location.getLatitude(),           location.getLongitude());

しかし、これは Android 2.3 で動作しますが、4.2 で動作します。null を返します。

4

2 に答える 2

0

私のアプリにはほとんど同じコードがあり、バージョン 4 でうまく動作します。

String provider;
LocationManager locManager = (LocationManager) 
                              getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locManager.getBestProvider(criteria, false);
Location location = locManager.getLastKnownLocation(provider);

if (location != null) {
   latitude = location.getLatitude();
   mLongitude = location.getLongitude();
}

ただし、問題は getMap() エラーに関連している必要があります。エラーメッセージは正確には何ですか?

于 2013-08-23T00:11:19.433 に答える