下了“深入浅出google android”这本书中的有关googlemap相关的几个demo源码,其中一个最简单的是获取当前经纬度,
就一个类,
public class CurrentLocation extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocationManager locationManager;
        String serviceName = Context.LOCATION_SERVICE;
        locationManager = (LocationManager)getSystemService(serviceName);
        //String provider = LocationManager.GPS_PROVIDER;
        
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        String provider = locationManager.getBestProvider(criteria, true);
        
        Location location = locationManager.getLastKnownLocation(provider);
        updateWithNewLocation(location);
        locationManager.requestLocationUpdates(provider, 2000, 10,
         locationListener);
    }
   private final LocationListener locationListener = new LocationListener() {
     public void onLocationChanged(Location location) {
     updateWithNewLocation(location);
     }
     public void onProviderDisabled(String provider){
     updateWithNewLocation(null);
     }
     public void onProviderEnabled(String provider){ }
     public void onStatusChanged(String provider, int status,
     Bundle extras){ }
    };
    private void updateWithNewLocation(Location location) {
     String latLongString;
     TextView myLocationText;
     myLocationText = (TextView)findViewById(R.id.myLocationText);
     if (location != null) {
     double lat = location.getLatitude();
     double lng = location.getLongitude();
     latLongString = "纬度:" + lat + "\n经度:" + lng;
     } else {
     latLongString = "无法获取地理信息";
     }
     myLocationText.setText("您当前的位置是:\n" +
     latLongString);
    }
}
我直接倒入到eclipse里,然后通过真机测试,竟然提示“无法获取地理信息”(G5,设置正常),模拟器测试也是“无法获取地理信息”,不知道怎么回事,请高手指点指点(相关源码

解决方案 »

  1.   

    我觉得这个是由于google的好多网站都被封了,别的baidu在模拟器上面都能运行,但是google则打不开
      

  2.   

    11-12 04:34:30.667: DEBUG/LocaleSetup(732): Update locate list
    11-12 04:34:30.692: DEBUG/ResourceType(732): calling getConfigurations
    11-12 04:34:30.735: DEBUG/ResourceType(732): called getConfigurations size=118
    11-12 04:34:30.737: DEBUG/asset(732): locale  0: ''
    11-12 04:34:30.747: DEBUG/asset(732): locale  1: 'ja'
    11-12 04:34:30.757: DEBUG/asset(732): locale  2: 'de'
    11-12 04:34:30.767: DEBUG/asset(732): locale  3: 'nl'
    11-12 04:34:30.767: DEBUG/asset(732): locale  4: 'pl'
    11-12 04:34:30.777: DEBUG/asset(732): locale  5: 'ko'
    11-12 04:34:30.798: DEBUG/asset(732): locale  6: 'fr'
    11-12 04:34:30.798: DEBUG/asset(732): locale  7: 'cs'
    11-12 04:34:30.798: DEBUG/asset(732): locale  8: 'es'
    11-12 04:34:30.798: DEBUG/asset(732): locale  9: 'it'
    11-12 04:34:30.798: DEBUG/asset(732): locale 10: 'ru'
    11-12 04:34:30.798: DEBUG/asset(732): locale 11: 'en_GB'
    11-12 04:34:30.798: DEBUG/asset(732): locale 12: 'en_SG'
    11-12 04:34:30.798: DEBUG/asset(732): locale 13: 'zh_CN'
    11-12 04:34:30.798: DEBUG/asset(732): locale 14: 'en_US'
    11-12 04:34:30.798: DEBUG/asset(732): locale 15: 'en_AU'
    11-12 04:34:30.798: DEBUG/asset(732): locale 16: 'zh_TW'
    11-12 04:34:30.798: DEBUG/asset(732): locale 17: 'en_CA'
    11-12 04:34:30.798: DEBUG/asset(732): locale 18: 'fr_CA'
    11-12 04:34:30.798: DEBUG/asset(732): locale 19: 'nl_BE'
    11-12 04:34:30.798: DEBUG/asset(732): locale 20: 'fr_BE'
    11-12 04:34:30.798: DEBUG/asset(732): locale 21: 'de_DE'
    11-12 04:34:30.798: DEBUG/asset(732): locale 22: 'de_CH'
    11-12 04:34:30.798: DEBUG/asset(732): locale 23: 'fr_CH'
    11-12 04:34:30.798: DEBUG/asset(732): locale 24: 'it_CH'
    11-12 04:34:30.798: DEBUG/asset(732): locale 25: 'de_LI'
    11-12 04:34:30.798: DEBUG/asset(732): locale 26: 'nl_NL'
    11-12 04:34:30.798: DEBUG/asset(732): locale 27: 'pl_PL'
    11-12 04:34:30.798: DEBUG/asset(732): locale 28: 'ja_JP'
    11-12 04:34:30.798: DEBUG/asset(732): locale 29: 'fr_FR'
    11-12 04:34:30.798: DEBUG/asset(732): locale 30: 'ko_KR'
    11-12 04:34:30.817: DEBUG/asset(732): locale 31: 'es_ES'
    11-12 04:34:30.817: DEBUG/asset(732): locale 32: 'de_AT'
    11-12 04:34:30.817: DEBUG/asset(732): locale 33: 'it_IT'
    11-12 04:34:30.817: DEBUG/asset(732): locale 34: 'ru_RU'
    11-12 04:34:30.817: DEBUG/asset(732): locale 35: 'cs_CZ'
    11-12 04:34:30.817: DEBUG/asset(732): locale 36: 'en_NZ'
    11-12 04:34:30.817: DEBUG/asset(732): locale 37: 'en'
    11-12 04:34:30.978: DEBUG/LocaleSetup(732): customLocales: 
    11-12 04:34:31.487: WARN/KeyCharacterMap(732): No keyboard for id 0
    11-12 04:34:31.607: WARN/KeyCharacterMap(732): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
    11-12 04:34:31.801: WARN/InputManagerService(571): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@437247b0 (uid=10004 pid=732)
    11-12 04:34:31.817: WARN/InputManagerService(571): Client not active, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@436d3418
    11-12 04:34:32.148: WARN/ActivityManager(571): Activity pause timeout for HistoryRecord{436bd608 {com.android.customlocale/com.android.customlocale.CustomLocaleActivity}}
    11-12 04:34:32.367: WARN/InputManagerService(571): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4371eb60
    11-12 04:34:33.797: DEBUG/dalvikvm(622): GC freed 1879 objects / 97256 bytes in 184ms
    11-12 04:34:36.517: INFO/ActivityManager(571): Starting activity: Intent { action=android.intent.action.MAIN categories={android.intent.category.LAUNCHER} flags=0x10200000 comp={com.studio.android.chp08.ex01/com.studio.android.chp08.ex01.CurrentLocation} }
    11-12 04:34:37.237: DEBUG/GpsLocationProvider(571): setMinTime 2000
    11-12 04:34:37.577: WARN/IInputConnectionWrapper(622): showStatusIcon on inactive InputConnection
    11-12 04:34:37.807: INFO/ActivityManager(571): Displayed activity com.studio.android.chp08.ex01/.CurrentLocation: 1241 ms
    11-12 04:34:42.958: DEBUG/dalvikvm(622): GC freed 147 objects / 6264 bytes in 122ms
    打log:
    11-12 04:39:31.838: DEBUG/dalvikvm(622): GC freed 227 objects / 10272 bytes in 155ms
    11-12 04:39:41.287: WARN/KeyCharacterMap(864): No keyboard for id 0
    11-12 04:39:41.307: WARN/KeyCharacterMap(864): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
    11-12 04:39:44.527: WARN/KeyCharacterMap(622): No keyboard for id 0
    11-12 04:39:44.538: WARN/KeyCharacterMap(622): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
    11-12 04:39:46.287: INFO/ActivityManager(571): Starting activity: Intent { action=android.intent.action.MAIN categories={android.intent.category.LAUNCHER} flags=0x10200000 comp={com.studio.android.chp08.ex01/com.studio.android.chp08.ex01.CurrentLocation} }
    11-12 04:39:46.798: DEBUG/location(864): locationnull
    11-12 04:39:46.818: DEBUG/GpsLocationProvider(571): setMinTime 2000
    11-12 04:39:47.247: WARN/IInputConnectionWrapper(622): showStatusIcon on inactive InputConnection
    11-12 04:39:47.437: INFO/ActivityManager(571): Displayed activity com.studio.android.chp08.ex01/.CurrentLocation: 1120 ms
    11-12 04:39:52.638: DEBUG/dalvikvm(622): GC freed 155 objects / 6952 bytes in 170ms
    好像检测不到信息
      

  3.   

    楼主的机器是有GPS模组的吗?
      

  4.   

    http://blog.csdn.net/sodino/archive/2010/10/12/5935307.aspx
    不知道上面这东西有帮助没
      

  5.   

    应该需要加载GPS模块里面的经纬过来吧,
      

  6.   

    跟这个没关系吧1、检查真机中是否开启了GPS服务,换句说,真机要有GPS的功能。。如果没有,要用基站定位。
    2、真机中不能设置为飞行模式,不然GPS没用。
    3、尽量在室处,卫星需要一些时间。
    4、检查代码。。
    5、对于模拟器中可以用DDMS模拟经纬度值唉,这个都搞烂了,大体就这么多问题。
      

  7.   

    哦,对了,我的测试环境为moto的里程碑
      

  8.   

    楼主,我也需要实现此功能,
    你能把此例子发一份给我email吗  [email protected]  thanks