わかりました、私はこのウェブサイトだけでなく、検索と読書に多くの時間を費やしました. ほとんどのスレッドは、GPS を扱ったり、エミュレーターで動作させようとしたりします。developer.android.com のドキュメントでは、ある程度までしか説明されていません。私は単純なことを働かせることができません。この問題を抱えているほとんどの人のように、ネットワーク プロバイダーから最後の既知の場所を取得したいだけで、Google マップを使用した直後でも取得できません (GPS がオフになっているためネットワーク プロバイダーを使用)。わずか1/8から1/4マイル離れています。さて、「プロバイダ」(オンの場合はセルタワー/Wi-Fi)がGoogleマップの修正を取得しています。アプリを試してみましたが、最後の既知の場所を取得できません。マップに表示しようとしているわけでもありません。ボタンを押して場所を取得した後、緯度と経度の座標を画面に出力しています。ドキュメントには、同じアクティビティから作成する必要がある最後の場所については何も記載されていません。指定されたプロバイダーから最後に認識された場所を返すと言われています。ドキュメントに記載されているNETWORK_PROVIDERは、セルタワーとwi-fiです。(そして、はい、私はそれをマニフェストでカバーしています。)
これがどのように機能するか知っている人はいますか?Google のサイトでさえ、私はまだ良い説明を見たことがありません。わからない場合は、イライラします。それは明らかに私がやっている(またはしていない)ことなので、私のニーズに対して少し短すぎるドキュメンテーションを除いて、私は自分以外の誰か/誰かを責めているわけではありません.
更新:私は現在、場所リスナーを使用して場所を取得しています。最後の場所を推測します。ロケーション リスナーを介してロケーションの更新を開始せずに最後のロケーションを取得できるのは理にかなっているように思えますが、そうではありませんでした。現在、ロケーション リスナーを使用しています。
今私の問題は、場所を変更できないことです。私のコードは、リスナーを介して位置の更新を取得しようとし、ボタンの押下を受け入れて最後の既知の位置を取得することを除いて、現在何もしません。Google マップには現在地が表示されますが、コードで 20 マイル離れた場所を取得し続けます。実際に新しい場所を取得する可能性があると感じていますが、必要なときにその場所を保存して使用するのを妨げている final / static 宣言の使用に誤りがある可能性があります。私のマニフェストには Internet と fine_location のアクセス許可があり、アクティビティの xml は正常に機能します。場所のライブ検索をオン/オフするボタン、lastknownlocation を手動で取得するボタン、緯度と経度を表示するテキストビューを提供するだけです。
public class RMain extends Activity {
public boolean islivesearchon;
public static double netlistentime = 0 * 60 * 1000; // minutes * 60 sec/min * 1000 for milliseconds
public static double netlistendistance = 0 * 1609.344; // miles * conversion to meters
public static double gpslistentime = 30 * 60 * 1000; // minutes * 60 sec/min * 1000 for milliseconds
public static double gpslistendistance = 0 * 1609.344; // miles * conversion to meters
private static Location currentlocation;
private LocationManager locationManager;
private LocationListener locationListener;
public static Criteria criteria;
public TextView latview;
public TextView longview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rmain);
latview = (TextView) findViewById(R.id.display_latitude);
longview = (TextView) findViewById(R.id.display_longitude);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// setup bestProvider
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestprovider = locationManager.getBestProvider(criteria, true);
// get an initial current location
currentlocation = locationManager.getLastKnownLocation(bestprovider);
latview.setText(Double.toString(currentlocation.getLatitude()));
longview.setText(Double.toString(currentlocation.getLongitude()));
// Define locationListener interface
locationListener = new LocListener();
// React to LiveSearch toggle button press
final Button livesearch = (Button) findViewById(R.id.button_livesearch);
livesearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView livesearchview = (TextView) findViewById(R.id.button_livesearch);
boolean isNetEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(islivesearchon) {
locationManager.removeUpdates(locationListener);
islivesearchon = false;
livesearchview.setText("Live Search is OFF");
}
else {
// ideally should check for !isProviderEnabled first, then provide option for turning it on
if(isNetEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, (long) netlistentime, (float) netlistendistance, locationListener);
islivesearchon = true;
livesearchview.setText("Live Search is ON");
}
// ideally should check for !isProviderEnabled first, then provide option for turning it on
if(isGpsEnabled) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, (long) gpslistentime, (float) gpslistendistance, locationListener);
islivesearchon = true;
livesearchview.setText("Live Search is ON");
}
}
}
});
// Respond to Get Location button click to get last known location
final Button getlocation = (Button) findViewById(R.id.button_getlocation);
getlocation.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String bestprovider = locationManager.getBestProvider(criteria, true);
Location lastlocation = locationManager.getLastKnownLocation(bestprovider);
latview.setText(Double.toString(lastlocation.getLatitude()));
longview.setText(Double.toString(lastlocation.getLongitude()));
}
});
}
private class LocListener implements LocationListener {
@Override
public void onLocationChanged(Location latestlocation) {
// Called when a new location is found by the network location provider.
currentlocation.set(latestlocation);
latview.setText(Double.toString(currentlocation.getLatitude()));
longview.setText(Double.toString(currentlocation.getLongitude()));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
};
}