AsyncTask を使ってGPS 位置情報を取得して、Google API サービスで住所文字列を取得する。
・AsyncTask で、LocationListener を実装する。
・LocationManager#requestLocationUpdates は、doInBackground で実行できないので、onPreExecute で実行する。
・HTTP Client で、GPS Locationで取得した緯度、経度をUriに埋め込みリクエストを投げる。
・json で住所を受け取るGoogle API サービスの Uri は、
http://maps.googleapis.com/maps/api/geocode/json?latlng=xxxx.xxxxxx,yyyyy.yyyyy&sensor=true&language=ja
xxxx.xxxxxx,yyyyy.yyyyy = 緯度+','+経度
・結果 json の formatted_address は、「国名,」が着いてしまうのでこれを取り除く
import java.io.ByteArrayOutputStream;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
/**
* GPS location -> address
*/
public class LocationAddressTask extends AsyncTask<Void,Void,String> implements LocationListener{
private Activity activity;
private Location mlocation;
private LocationManager locationManager;
public LocationAddressTask(Activity activity){
this.activity = activity;
}
@Override
protected final void onPreExecute(){
locationManager = (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,this);
}
@Override
protected final String doInBackground(Void...params){
String addressString = null;
while(mlocation==null){
try{ Thread.sleep(100); }catch(Exception e){}
}
locationManager.removeUpdates(this);
HttpClient httpclient = new DefaultHttpClient();
String uri = "http://maps.googleapis.com/maps/api/geocode/json"
+ "?latlng=" + Double.toString(mlocation.getLatitude())
+ "," + Double.toString(mlocation.getLongitude())
+ "&sensor=true&language=ja";
HttpGet httpget = new HttpGet(uri.toString());
try{
HttpResponse response = httpclient.execute(httpget);
if (response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
ByteArrayOutputStream ost = new ByteArrayOutputStream();
response.getEntity().writeTo(ost);
String str = ost.toString();
ost.close();
addressString = parseAddress(str);
}
}catch(Exception e){
Logger.w(e.getMessage(), e);
}
return addressString;
}
private String parseAddress(String jsonstr) throws JSONException{
JSONObject json = new JSONObject(jsonstr);
JSONArray array = json.getJSONArray("results");
JSONArray address_components;
String addressString = null;
String formatted_address = null;
for(int i=0;i < array.length();i++){
JSONObject jsonObject = array.getJSONObject(i);
address_components = jsonObject.getJSONArray("address_components");
formatted_address = jsonObject.getString("formatted_address");
if (!"".equals(formatted_address)){
for(int n=0;n < address_components.length();n++){
JSONArray types = address_components.getJSONObject(n).getJSONArray("types");
if (types.length() > 1){
if ("country".equals(types.getString(0)) && "political".equals(types.getString(1))){
String country = address_components.getJSONObject(n).getString("long_name") + ", ";
addressString = formatted_address.replace(country, "").replaceAll("\\?", "-");
break;
}
}
}
if (addressString != null) break;
}
}
return addressString;
}
@Override
protected final void onPostExecute(String result){
// 取得した住所を使用する
}
@Override
protected void onCancelled(){
super.onCancelled();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location){
mlocation = location;
}
@Override
public void onProviderDisabled(String provider){
}
@Override
public void onProviderEnabled(String provider){
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras){
}
}