获得用户Geopoint

时间:2012-07-25 08:05:49

标签: android

我正在尝试使用此代码获取用户geopoint

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

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

2 个答案:

答案 0 :(得分:3)

如果您在此处获得异常,似乎您正在使用模拟器进行测试。

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

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

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

答案 1 :(得分: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);

它对我有用。