0
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 

上記のコードで場所が変更された場合、onLocationChanged関数が完全に機能していることがわかります。

///////CLASS  mylocationlistener
private class mylocationlistener implements LocationListener {
    //@Override
    public void onLocationChanged(Location location) {
        if (location != null) {
        Log.d("LOCATION CHANGED", location.getLatitude() + "");
        Log.d("LOCATION CHANGED", location.getLongitude() + "");
        Toast.makeText(MainActivity.this,
            location.getLatitude() + "" + location.getLongitude(),
            Toast.LENGTH_LONG).show();
        p = new GeoPoint((int)location.getLatitude(),(int)location.getLongitude());

       // p = new GeoPoint((int)8.538754,(int)76.950620);
        }
    }
    //@Override
    public void onProviderDisabled(String provider) {
    }
   // @Override
    public void onProviderEnabled(String provider) {
    }
   // @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    }

しかし、問題は、変数「p」に現在の場所、つまりプログラムの開始時、つまり最初の変更の前にヘルプを入力したいことです!!

4

2 に答える 2

0

修正する前に、粗い場所を使用できます。このリンクを確認してください:http: //devdiscoveries.wordpress.com/2010/02/04/android-use-location-services/

于 2012-09-28T06:50:27.923 に答える
0

このコードを試してください:

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.");
  onLocationChanged(location);
} else {
  latituteField.setText("Location not available");
  longitudeField.setText("Location not available");
  System.out.println("Location not avilable");
}
}

/* 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) {
double lat = (double) (location.getLatitude());
double lng = (double) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
Log.i(TAG, "Lattitude:" +lat);
Log.i(TAG, "Longitude:" +lng);
}
于 2012-09-28T05:28:26.730 に答える