没有获得当前的GPS值

时间:2012-03-28 06:12:17

标签: android google-maps gps

我正在做一个需要当前GPS值的小应用程序。为此,我阅读了很多教程,并尝试使用此sample code。但我没有得到价值观。当我运行代码时,它强行关闭应用程序。这是我的logcat。

03-27 12:57:49.881: E/AndroidRuntime(604): FATAL EXCEPTION: main
03-27 12:57:49.881: E/AndroidRuntime(604): java.lang.UnsatisfiedLinkError: onCreate
03-27 12:57:49.881: E/AndroidRuntime(604):  at com.google.android.maps.MapActivity.onCreate(Native Method)
03-27 12:57:49.881: E/AndroidRuntime(604):  at com.android.and.AndActivity.onCreate(AndActivity.java:48)
03-27 12:57:49.881: E/AndroidRuntime(604):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-27 12:57:49.881: E/AndroidRuntime(604):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-27 12:57:49.881: E/AndroidRuntime(604):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-27 12:57:49.881: E/AndroidRuntime(604):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-27 12:57:49.881: E/AndroidRuntime(604):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-27 12:57:49.881: E/AndroidRuntime(604):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 12:57:49.881: E/AndroidRuntime(604):  at android.os.Looper.loop(Looper.java:123)
03-27 12:57:49.881: E/AndroidRuntime(604):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-27 12:57:49.881: E/AndroidRuntime(604):  at java.lang.reflect.Method.invokeNative(Native Method)
03-27 12:57:49.881: E/AndroidRuntime(604):  at java.lang.reflect.Method.invoke(Method.java:521)
03-27 12:57:49.881: E/AndroidRuntime(604):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-27 12:57:49.881: E/AndroidRuntime(604):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-27 12:57:49.881: E/AndroidRuntime(604):  at dalvik.system.NativeStart.main(Native Method)

修改

这是我的活动。

public class AndActivity extends Activity implements LocationListener {
    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager locationManager;
    private String provider;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        latituteField = (TextView) findViewById(R.id.TextView02);
        longitudeField = (TextView) findViewById(R.id.TextView04);

        // Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

        // Initialize the location fields
        if (location != null) {
            System.out.println("Provider " + provider + " has been selected.");
            int lat = (int) (location.getLatitude());
            int lng = (int) (location.getLongitude());
            latituteField.setText(String.valueOf(lat));
            longitudeField.setText(String.valueOf(lng));
        } else {
            latituteField.setText("Provider not available");
            longitudeField.setText("Provider not available");
        }
    }

    /* Request updates at startup */
    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
    }

    /* Remove the locationlistener updates when Activity is paused */
    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {

        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
                Toast.LENGTH_SHORT).show();
    }
}

4 个答案:

答案 0 :(得分:1)

获取当前GPS坐标的一些有用链接。

http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/

http://www.oudmaijer.com/2010/04/15/android-retrieving-your-current-location/

http://developer.android.com/guide/topics/location/obtaining-user-location.html

http://about-android.blogspot.in/2010/04/find-current-location-in-android-gps.html

http://www.android10.org/index.php/articleslocationmaps/209-obtaining-user-location

http://www.android10.org/index.php/articleslocationmaps/226-android-location-providers-gps-network-p

您可以找到有关获取用户当前GPS坐标的更简单明了的信息。一些教程也在那里。希望这会对你有所帮助。

修改

试试这个......

public class gpsactivity {

    private static final int gpsMinTime = 6000;
    private static final int gpsMinDistance = 1;

    private LocationManager locationManager = null;
    private LocationListener locationListener = null;
    private Location location;
    private String bestProvider;
    private GPSCallback gpsCallback = null;
    private GeomagneticField geoField;

    public gpsactivity() {
        locationListener = new LocationListener() {
            public void onProviderDisabled(final String provider) {
            }

            public void onProviderEnabled(final String provider) {
            }

            public void onLocationChanged(final Location location) {
                if (location != null && gpsCallback != null) {
//                  gpsCallback.onGPSUpdate(location);

                    geoField = new GeomagneticField(
                             Double.valueOf(location.getLatitude()).floatValue(),
                             Double.valueOf(location.getLongitude()).floatValue(),
                             Double.valueOf(location.getAltitude()).floatValue(),
                             System.currentTimeMillis()
                          );
                }
            }

            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                // TODO Auto-generated method stub
            }
        };
    }

    public Location getCurrentLocation() {

        if (locationManager != null && bestProvider != null
                && bestProvider.length() > 0) {
            location = locationManager.getLastKnownLocation(bestProvider);
        }
        return location;
    }

通过

调用此类
 gpsManager = new gpsactivity();            
    location = gpsManager.getCurrentLocation();

    if (gpsManager != null) 
            {
                Longitude = location.getLongitude();
                Latitude = location.getLatitude();


                if (location != null) 
                {
                    location.setLatitude(Latitude);
                    location.setLongitude(Longitude);           
                }
            }

            intLatitude = (int) (Latitude * 1000000);
            intLongitude = (int) (Longitude * 1000000);  

...谢谢

答案 1 :(得分:0)

试试这个 从中删除实现LocationListener 公共类AndActivity扩展Activity实现LocationListener

并创建单独的类  公共类MyLocationListener实现LocationListener

并在此类中实现覆盖函数。

答案 2 :(得分:0)

嗨,我的手机代码正常运行。代码在我的模拟器中不起作用。当我在手机上安装它时它工作正常。 感谢您的回复。

答案 3 :(得分:0)

嘿沃尔沃林说得对..

模拟器我们没有获得Curent GPS位置..您必须并且应该在手机中安装您的应用程序..

由您首先打开手机中的GPS状态。

点击此链接.. click link

我已经尝试过并在Android手机上进行测试..它正在工作。

度过愉快的一天。