4

私のアプリでは、ユーザーが場所を入力し、その場所の緯度と経度を取得する必要があります。Locationしたがって、ここにはオブジェクトはありませんが、 String.

Locationこれを使用してオブジェクトを作成するオプションを考えましたが、String不可能です。

私は Google の Places API を使用できることを知っており、その使用方法も知っていますが、SDK で利用可能な方法を使用することを好みます。

そうする方法はありますか?

4

7 に答える 7

4

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;
}

幸運をお祈りしています。

于 2013-06-24T13:04:01.997 に答える
0

あなたは見ることができます:

http://developer.android.com/reference/android/location/Geocoder.html

getFromLocationName メソッドが役立ちます。

于 2013-06-24T13:00:04.527 に答える
0

Google Geocoding API を使用して、住所の座標を取得します。http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding

この詳細には、使用方法に関する情報が記載されています。また、以下にサンプル URL を示します。応答を確認して、これが役立つかどうかを確認してください。

http://maps.googleapis.com/maps/api/geocode/json?address=ahmedab​​ad&sensor=false

于 2013-06-24T13:18:00.280 に答える
-1

あなたはこれを使うことができます、

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
geocoder.getFromLocation(LATITUDE, LONGITUDE, 3);
于 2013-06-24T13:00:41.463 に答える