1

我能够在 Galaxy pop、galaxy y 中获取当前位置的当前纬度和经度。但我无法在 HTC explore 中获得当前位置?

我使用的编码如下:

    @Override
    protected void onStart() {

            // TODO Auto-generated method stub

            super.onStart();
            context = Context.LOCATION_SERVICE;
            locationManager = (LocationManager)getSystemService(context);
            locationManager.requestLocationUpdates(provider, 1, 1, locationListener);
            crta = new Criteria();
            crta.setAccuracy(Criteria.ACCURACY_FINE);
            crta.setAltitudeRequired(false);
            crta.setBearingRequired(false);
            crta.setCostAllowed(true);
            crta.setPowerRequirement(Criteria.POWER_LOW);
            //String provider = locationManager.getBestProvider(crta, true);
             String provider = LocationManager.GPS_PROVIDER;
             location = locationManager.getLastKnownLocation(provider);
             updateWithNewLocation(location);
            }

            private void updateWithNewLocation(Location location) {
                        String latLong;             
`enter code here`//                      myLocation = (TextView) findViewById(R.id.myLocation);
                        String addressString = "no address found";


                        if(location!=null) {
            System.out.println("Provider " + provider + " has been selected.");
            lat  = (Double) (location.getLatitude());
            lng  = (Double) (location.getLongitude()); 
        // Gomathi
                                    double valueofGPSlat = lat * 1000;
                                    int GPSsum = (int) valueofGPSlat;
                                    GPSvalue = (double) GPSsum / 1000;


                                    // Get Current Location
                                    latt = Double.toString(GPSvalue);                                                                                                                                                         
                                    // Getlon
                                    double valueofGPSlng = lng * 1000;
                                    int resultGPSsum = (int) valueofGPSlng;
                                    resultGPSvalue = (double) resultGPSsum / 1000;
                                    longt = Double.toString(resultGPSvalue);
            // GomathiEnd
                        } else {
                        latLong = " NO Location Found ";
                        }
                        //myLocation.setText("your Current Position is :\n" +latLong + "\n " + addressString );

                        Toast.makeText(UsertaskoptionsActivity.this,"your Current Position is :\n" +latt + "\n " + ""+longt , Toast.LENGTH_LONG).show();
                        }

            private LocationListener locationListener = new LocationListener()
            {
            @Override
            public void onLocationChanged(Location location) {
            updateWithNewLocation(location);
            }

            private void updateWithNewLocation(Location location) {
                        // TODO Auto-generated method stub
            }

            @Override
            public void onProviderDisabled(String provider) {          
            updateWithNewLocation(null);
            }

            @Override
            public void onProviderEnabled(String provider) {

            }

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

            }
            };
4

1 回答 1

0

如果相应的提供商尚未检索到一个位置,您将无法获得最后一个已知位置。您可以尝试运行其他一些首先使用 gps 的应用程序。通常,您应该将您的应用注册到位置更新,请查看有关“获取用户位置”的 android 开发指南http://developer.android.com/guide/topics/location/obtaining-user-location.html

于 2012-05-29T06:29:19.093 回答