ユーザーの現在の位置に基づいて最も近い場所を取得する GPS Android アプリケーションを構築しています。
これは私のアプリケーションが行うことです:
- GPS またはネットワークが利用可能かどうかを確認する
- どちらも利用できない場合は、何もしないでください。それ以外の場合は、最初に GPS があるかどうかを確認し、そうでない場合はネットワークを確認します。
- それらのいずれかを使用した後、現在の場所を取得してサーバーに送信します。
- サーバーからデータを取得して UI を更新すると、それ以降の場所の更新のリッスンを停止します。もう一度開始する更新ボタンを押すまでは、1 回で十分です。
私がやりたいこと:
GPS またはネットワークが場所の取得に失敗した場合 (たとえば 2 分)、プロバイダーを切り替えます。ユーザーを長時間待たせたくありません。
また、両方のプロバイダーを使用して、そこから最も正確なものを取得できるとよいでしょう。http://developer.android.com/guide/topics/location/strategies.htmlを見て、isBetterLocation メソッドを見ました。このメソッドをアプリケーションにどのように統合しますか? どのように、どこで、いつ呼び出す必要があるのか 理解できません。isBetterLocation() では、Network と GPS の両方を同時に呼び出す必要があると思います。私のアプリケーションが正確さのために両方をリッスンするのはいいことです。どうすればいいですか?それらのいずれかが利用できない場合はどうなりますか?
これが私のコードの一部です:
if(!GPSEnabled && !networkEnabled)
{
Toast.makeText(this, "Error: This application requires a GPS or network connection",
Toast.LENGTH_SHORT).show();
}
else
{
if(GPSEnabled)
{
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
else if(networkEnabled)
{
System.out.println("Getting updates from network provider");
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
}
これがonLocationChanged
方法です。緯度/経度の値を取得してサーバーに送信し、適切な処理を行います。
public void onLocationChanged(Location location)
{
//Get coordinates
double lat = (location.getLatitude());
double lng = (location.getLongitude());
Log.d("MainActivity", "got location: " + lat + ": " + lng);
//get nearest locations
new GetLocations().execute(SharedVariables.root + SharedVariables.locationsController + SharedVariables.getNearestMethod + lat + "/" + lng);
// Zoom in, animating the camera after the markers have been placed
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 10));
System.out.println("lat = " + lat + ", lng = " + lng);
//Stop listening for updates. We only want to do this once.
locManager.removeUpdates(this);
}