2011-03-09 47 views

回答

1

試試這個

public class MyLocationOnMap extends MapActivity { 

     private LocationManager hdLocMgr; 
     private String hdLocProvider; 


     onCreate(...) { 
      : 
     Criteria hdCrit = new Criteria(); 
     hdCrit.setAccuracy(Criteria.ACCURACY_COARSE); 
     hdCrit.setAltitudeRequired(false); 
     hdCrit.setBearingRequired(false); 
     hdCrit.setCostAllowed(true); 
     hdCrit.setPowerRequirement(Criteria.POWER_LOW); 

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

     hdLocProvider = hdLocMgr.getBestProvider(hdCrit, true); 

     Location location = hdLocMgr.getLastKnownLocation(hdLocProvider); 

     Double dlat = location.getLatitude(); 
     Double dlon = location.getLongitude(); 
       : 
     } 

} 
+0

位置得到NULL :( – indira 2011-03-09 08:11:44

+0

我可以在模擬器當前位置? – indira 2011-03-09 08:58:21

+0

是的,你可以 - 進入http://developer.android.com/guide/developing/debugging/ddms.html並搜索「設置手機的位置」 – Abhi 2011-03-11 01:35:52

1

使用位置管理器,您可以:獲取您的當前位置

選擇一個位置提供

String providerName = LocationManager.GPS_PROVIDER; 
LocationProvider gpsProvider; 
gpsProvider = locationManager.getProvider(providerName); 

查找位置提供使用標準

Criteria criteria = new Criteria(); 
criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
criteria.setPowerRequirement(Criteria.POWER_LOW); 
criteria.setAltitudeRequired(false); 
criteria.setBearingRequired(false); 
criteria.setSpeedRequired(false); 
Criteria.setCostAllowed(true); 

String bestProvider = locationManager.getBestProvider(criteria, true); 

Location location = 
     locationManager.getLastKnownLocation(bestProvider); 
    updateWithNewLocation(location); 

    locationManager.requestLocationUpdates(bestProvider , 2000, 10, 
      locationListener); 

詳情,請訪問:http://developer.android.com/reference/android/location/LocationManager.html

+0

我想你還沒有正確讀取的問題 – ingsaurabh 2011-03-09 04:55:39

+1

葉,似乎很多白癡的想給垃圾答案,甚至沒有閱讀評論 – ericlee 2011-11-17 12:59:51