私のアプリケーションでは、デフォルト値をlatitude
とに設定していますlongitude
。入力するときは、cityname in edittext
そのポイントを必要な場所に移動する必要があります
4 に答える
以下のコードを使用して、都市名の緯度と経度を取得します
String location=cityName;
String inputLine = "";
String result = ""
location=location.replaceAll(" ", "%20");
String myUrl="http://maps.google.com/maps/geo?q="+location+"&output=csv";
try{
URL url=new URL(myUrl);
URLConnection urlConnection=url.openConnection();
BufferedReader in = new BufferedReader(new
InputStreamReader(urlConnection.getInputStream()));
while ((inputLine = in.readLine()) != null) {
result=inputLine;
}
String lat = result.substring(6, result.lastIndexOf(","));
String longi = result.substring(result.lastIndexOf(",") + 1);
}
catch(Exception e){
e.printStackTrace();
}
私の提案は、GeocoderではなくGoogle GeoCodingRESTAPIを使用することです。
私はかつて私の国、中国でそれを使用したので、私は以下のエラーがあります:
"java.io.IOException: Service not Available"
検索後、そうです
"The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists."
そこで、Google GeoCoding REST APIに目を向けます。これは簡単です。次のようにリクエストするだけです:
http://maps.googleapis.com/maps/api/geocode/json?address=NewYork&sensor=false
ジオロケーションが含まれるjsonを取得します。また、locaitonからアドレスを取得することもできます。
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false
あなたが中国語を読むことができるなら、私はこれについてのブログを持っています、これをチェックしてください: http ://wangchao.de/android%E5%88%A9%E7%94%A8google-geocoding-api%E6%9B%BF%E4 %BB%A3geocoder-%E8%A7%A3%E5%86%B3%E5%9C%B0%E5%9D%80%E8%AF%AD%E8%A8%80%E9%97%AE%E9% A2%98
Geocoderクラスを使用します。
Geocode gc = new Geocoder();
List<Address> ads = gc.getFromLocationName(cityName,maxResults);
ドキュメントから:
ジオコーディングと逆ジオコーディングを処理するためのクラス。ジオコーディングは、住所または場所の他の説明を(緯度、経度)座標に変換するプロセスです。逆ジオコーディングは、(緯度、経度)座標を(部分的な)アドレスに変換するプロセスです。逆ジオコーディングされた場所の説明の詳細の量は異なる場合があります。たとえば、1つには最寄りの建物の完全な住所が含まれ、もう1つには都市名と郵便番号のみが含まれる場合があります。Geocoderクラスには、コアandroidフレームワークに含まれていないバックエンドサービスが必要です。プラットフォームにバックエンドサービスがない場合、Geocoderクエリメソッドは空のリストを返します。isPresent()メソッドを使用して、ジオコーダーの実装が存在するかどうかを確認します。
citynameのGeoPointを取得してみてください
GeoPoint startGP = new GeoPoint(
(int) (Double.parseDouble(cityname.getText()) * 1E6));
次に、以下のコードを使用して、GeoPointから緯度と経度を取得できます。
Double.toString((double)startGP.getLatitudeE6() / 1E6),
Double.toString((double)dest.getLongitudeE6() / 1E6)