私のアプリでは、ユーザーが場所を入力し、その場所の緯度と経度を取得する必要があります。Location
したがって、ここにはオブジェクトはありませんが、 String
.
Location
これを使用してオブジェクトを作成するオプションを考えましたが、String
不可能です。
私は Google の Places API を使用できることを知っており、その使用方法も知っていますが、SDK で利用可能な方法を使用することを好みます。
そうする方法はありますか?
私のアプリでは、ユーザーが場所を入力し、その場所の緯度と経度を取得する必要があります。Location
したがって、ここにはオブジェクトはありませんが、 String
.
Location
これを使用してオブジェクトを作成するオプションを考えましたが、String
不可能です。
私は Google の Places API を使用できることを知っており、その使用方法も知っていますが、SDK で利用可能な方法を使用することを好みます。
そうする方法はありますか?
Geocoderを使用できます。このような :
Geocoder geocoder = new Geocoder(App.getContext(), Locale.US);
List<Address> listOfAddress;
try {
listOfAddress = geocoder.getFromLocation(theLatitude, theLongitude, 1);
if(listOfAddress != null && !listOfAddress.isEmpty()){
Address address = listOfAddress.get(0);
String country = address.getCountryCode();
String adminArea= address.getAdminArea();
String locality= address.getLocality();
}
} catch (IOException e) {
e.printStackTrace();
}
Updated :アドレスから場所を取得するには、次を使用します。
listOfAddress = geocoder.getFromLocationName("Address", 1);
そして緯度、経度を取得するには:
double latitude = address.getLatitude();
double longitude = address.getLongitude();
更新: Geocoder が null を返したときに、このスニペットを使用して場所を取得します。
private static final String URL = "http://maps.googleapis.com/maps/api/geocode/xml";
public static RemoteCommand getLocation(String theAddress){
RemoteCommand result = new RemoteCommand();
result.setType(Type.GET);
result.setUrl(URL);
result.addGetParam("address", theAddress);
result.addGetParam("sensor", "false");
result.addGetParam("language", "en");
// See description about GeocoderXMLHandler
result.setXmlHandler(new GeocoderXMLHandler());
return result;
}
幸運をお祈りしています。
あなたは見ることができます:
http://developer.android.com/reference/android/location/Geocoder.html
getFromLocationName メソッドが役立ちます。
Google Geocoding API を使用して、住所の座標を取得します。http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding
この詳細には、使用方法に関する情報が記載されています。また、以下にサンプル URL を示します。応答を確認して、これが役立つかどうかを確認してください。
http://maps.googleapis.com/maps/api/geocode/json?address=ahmedabad&sensor=false
あなたはこれを使うことができます、
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
geocoder.getFromLocation(LATITUDE, LONGITUDE, 3);