0

次のコードを使用して、ユーザーの現在の場所を見つけています

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);

public void onLocationChanged(Location location) {
    Log.e("changed","location");
    // TODO Auto-generated method stub
    showLocation(location); 
}

しかし、ユーザーの場所が見つかりません.プロバイダーをネットワークプロバイダーに変更すると、機能します.しかし、GPSプロバイダーのみでは機能しません.

4

4 に答える 4

0

まず、GPS が有効または無効になっていることを確認します。GPS を有効にした後、ロケーション リスナーを使用して現在の位置を取得します。次のコードを使用して GPS の位置を取得しています

String sourceLat, sourceLong;
LocationManager locManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);        

    txtCurrLat = (TextView) findViewById(R.id.txtCurrLat);
    txtCurrLong = (TextView) findViewById(R.id.txtCurrLong);

    mapView = (MapView) findViewById(R.id.mapview);


    geoPoint = null;
    mapView.setSatellite(false);

    locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f, locationListener);
    Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if(location != null)                                
    {
        double fromLat = location.getLatitude();
        double fromLong = roundTwoDecimals(location.getLongitude());

        Toast.makeText(getApplicationContext(), fromLat +fromLong+""  , Toast.LENGTH_SHORT).show();
        sourceLat = Double.toString(fromLat);
        sourceLong = Double.toString(fromLong);

        txtCurrLat.setText(sourceLat+","+sourceLong);   

        btnShow.setOnClickListener(this);
    }else{

        Toast.makeText(getApplicationContext(), "Location is not avilable", Toast.LENGTH_SHORT).show();
    }





}

private void updateWithNewLocation(Location location) {

    String latLongString = "";
    if (location != null) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        sourceLat = Double.toString(lat);
        sourceLong = Double.toString(lng);
        latLongString =  sourceLat + "," + sourceLong;
    } else {
        latLongString = "No location found";
        Toast.makeText(getApplicationContext(), latLongString+"", Toast.LENGTH_SHORT).show();
    }
    txtCurrLat.setText(latLongString);
}

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

    public void onProviderDisabled(String provider) {
        Toast.makeText( getApplicationContext(), "Gps is Disabled.Please enabale gps", Toast.LENGTH_SHORT ).show();
        updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider) {
        Toast.makeText( getApplicationContext(), "Gps is Enabled", Toast.LENGTH_SHORT).show();
    }

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

    }
};
于 2012-07-10T09:48:39.150 に答える
0

GPS には、「オンデマンドで現在地を取得する」機能はありません。デバイスがいくつかの衛星から修正を取得するまで、辛抱強く待つ必要があります。空がはっきりと見える場合でも、これには数分かかる場合があります。これが、OS がこれをコールバックとして実装する理由です。onLocationChanged は、デバイスが修正されたときに実行され、それを待つだけです。

于 2012-07-10T09:52:31.247 に答える