3

AndroidとGoogleマップをAPIレベル10で使用しており、緯度と経度をtelnetで取得しています。

public void onLocationChanged(Location location) {
    lat = location.getLatitude();
    lng = location.getLongitude();
    //...
}

ただし、ユーザーが最初に移動する必要があります。他に解決策はありますか?

4

1 に答える 1

1

onLocationChanged(Location location)の代わりに、ロケーションマネージャーとブロードキャストレシーバーを使用してユーザーのロケーション情報を取得することもできます。Location Managerクラスの詳細については、ドキュメントを参照してください。ロケーションマネージャークラスを使用すると、ユーザーが移動していない場合でも、ユーザーのロケーションを調査する間隔を設定できます。あなたが持っているでしょう:

LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

Intent intent = new Intent(context, LocationReceiver.class);
            pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            List<String> knownProviders = locationManager.getAllProviders();

            if(locationManager != null && pendingIntent != null && knownProviders.contains(LocationManager.GPS_PROVIDER)){
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINUTE_INTERVAL*5, 0, pendingIntent);
            }

            if(locationManager != null && pendingIntent != null && knownProviders.contains(LocationManager.NETWORK_PROVIDER)){
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINUTE_INTERVAL*5, 0, pendingIntent);
            }

LocationReceiverクラス(Broadcast Receiver)には、以下を使用できるonRecieve()メソッドがあります。

public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        location = (Location) bundle.get(LocationManager.KEY_LOCATION_CHANGED);

そのロケーションオブジェクトはさまざまな用途に使用できます 。Androidロケーションのドキュメントを参照してください。

于 2012-06-16T00:41:20.957 に答える