1

我正在尝试使用此代码获取用户地理点

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

        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
               GeoPoint test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getAltitude()*1000000);
               MapView mapview = (MapView) findViewById(R.id.mvmap);
               MapController mapcontrol = mapview.getController();
               mapcontrol.animateTo(test);
               mapview.setStreetView(true);
               mapview.setSatellite(true);
                mapview.setBuiltInZoomControls(true);
            }


            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }
        };
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

但我收到一个这样的错误。

    07-25 16:02:42.525: E/AndroidRuntime(7262): FATAL EXCEPTION: main
07-25 16:02:42.525: E/AndroidRuntime(7262): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.frux.kfcmobile/com.frux.kfcmobile.MapViewActivity}: java.lang.IllegalArgumentException: requested provider network doesn't exisit
07-25 16:02:42.525: E/AndroidRuntime(7262):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at android.os.Looper.loop(Looper.java:137)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at java.lang.reflect.Method.invokeNative(Native Method)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at java.lang.reflect.Method.invoke(Method.java:511)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at dalvik.system.NativeStart.main(Native Method)
07-25 16:02:42.525: E/AndroidRuntime(7262): Caused by: java.lang.IllegalArgumentException: requested provider network doesn't exisit
07-25 16:02:42.525: E/AndroidRuntime(7262):     at android.os.Parcel.readException(Parcel.java:1429)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at android.os.Parcel.readException(Parcel.java:1379)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:646)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at android.location.LocationManager._requestLocationUpdates(LocationManager.java:660)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:482)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at com.frux.kfcmobile.MapViewActivity.onCreate(MapViewActivity.java:56)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at android.app.Activity.performCreate(Activity.java:5008)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-25 16:02:42.525: E/AndroidRuntime(7262):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-25 16:02:42.525: E/AndroidRuntime(7262):     ... 11 more

任何人都可以找出导致错误的原因吗?

4

2 回答 2

3

看来您正在使用模拟器进行测试,如果是,您在这里遇到异常。

 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

我不认为你不能在模拟器上测试网络提供商。你可以像这样用 gps 测试它

 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
于 2012-07-25T08:20:50.990 回答
0

采用

GeoPoint test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getLongitude()*1000000);

代替

 GeoPoint test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getAltitude()*1000000);

即使用getLongitude()而不是getAltitude().

要显示确切的位置使用,

int lat = (int) (location.getLatitude() * 1E6);
          int lng = (int) (location.getLongitude() * 1E6);
          GeoPoint test= new GeoPoint(
        lat,lng);

这个对我有用。

于 2012-07-25T08:11:57.340 回答