私はマップAPIを使用して私のアプリで逆ジオコーディングをしたいですが、私は正確にその方法を知っていませんか?Googleマップのジオコードを逆にする方法2 android
答えて
使用Geocoder:
Geocoder geoCoder = new Geocoder(context);
List<Address> matches = geoCoder.getFromLocation(latitude, longitude, 1);
Address bestMatch = (matches.isEmpty() ? null : matches.get(0));
これは、それが私のためにどのように動作するか..です
MarkerOptions markerOptions;
Location myLocation;
Button btLocInfo;
String selectedLocAddress;
private GoogleMap myMap;
LatLng latLng;
LatLng tmpLatLng;
@Override
public void onMapLongClick(LatLng point) {
// Getting the Latitude and Longitude of the touched location
latLng = point;
// Clears the previously touched position
myMap.clear();
// Animating to the touched position
myMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Creating a marker
markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng);
// Adding Marker on the touched location with address
new ReverseGeocodingTask(getBaseContext()).execute(latLng);
//tmpLatLng = latLng;
btLocInfo.setEnabled(true);
btLocInfo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
double[] coordinates={tmpLatLng.latitude/1E6,tmpLatLng.longitude/1E6};
double latitude = tmpLatLng.latitude;
double longitude = tmpLatLng.longitude;
Log.i("selectedCoordinates", latitude + " " + longitude);
Log.i("selectedLocAddress", selectedLocAddress);
}
});
}
private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String>{
Context mContext;
public ReverseGeocodingTask(Context context){
super();
mContext = context;
}
// Finding address using reverse geocoding
@Override
protected String doInBackground(LatLng... params) {
Geocoder geocoder = new Geocoder(mContext);
double latitude = params[0].latitude;
double longitude = params[0].longitude;
List<Address> addresses = null;
String addressText="";
try {
addresses = geocoder.getFromLocation(latitude, longitude,1);
Thread.sleep(500);
if(addresses != null && addresses.size() > 0){
Address address = addresses.get(0);
addressText = String.format("%s, %s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getLocality(),
address.getCountryName());
}
}
catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
selectedLocAddress = addressText;
return addressText;
}
@Override
protected void onPostExecute(String addressText) {
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(addressText);
// Placing a marker on the touched position
myMap.addMarker(markerOptions);
}
}
次の2つの方法
- で逆ジオコーディングを行うことができますジオコーダ
- グーグルAPI
それは別のスレッドで実行されなければならない ジオコーダ:
private class FindPlaces extends AsyncTask<String, Void, List<Address>> {
@Override
protected List<Address> doInBackground(String... params) {
if (act == null)
this.cancel(true);
Geocoder geocoder = new Geocoder(act, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(
Double.parseDouble(params[0]),
Double.parseDouble(params[1]), result);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
@Override
protected void onPostExecute(List<Address> addresses) {
super.onPostExecute(addresses);
if (act == null)
return;
if (addresses == null || addresses.size() == 0) {
Toast.makeText(act, "No location found", Toast.LENGTH_SHORT)
.show();
return;
}
address = addresses.get(0);
String aLine = "";
for (int addr = 0; addr <= address.getMaxAddressLineIndex() - 2; addr++) {
aLine = aLine.length() > 0 ? aLine + ", "
+ String.valueOf(address.getAddressLine(addr)) : String
.valueOf(address.getAddressLine(addr));
}
address.setAddressLine(0, aLine);
if (act == null)
return;
}
}
のGoogle API 1) 2)このURL https://maps.googleapis.com/maps/api/geocode/json?latlng=であなたのLatLongブログを連結し、GoogleのコンソールでGoogleマップのジオコーディングAPIを有効にします
例: https://maps.googleapis.com/maps/api/geocode/json?latlng=youlatitude,yourlongitude&key=yourapikey
iがジオコーディングし、それのためにこれを行っているworks.My問題は、私はマップのポイントが取得geocoding.Fromを逆にやりたいということです
public class ReverseGecoding extends AsyncTask<Double, Void, String> {
Context context;**
private Address address;
private String GEOCODINGKEY = "&key=YourKey";
private String REVERSE_GEOCODING_URL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=";
public ReverseGecoding(Context context) {
this.context = context;
this.listener = listener;
}
@Override
protected String doInBackground(Double... params) {
if (params[0] != null) {
String result = "";
try {
String mUrl = REVERSE_GEOCODING_URL + params[0] + ","
+ params[1] + GEOCODINGKEY;
URL url = new URL(mUrl);
HttpURLConnection httpsURLConnection = (HttpURLConnection) url.openConnection();
httpsURLConnection.setReadTimeout(10000);
httpsURLConnection.setConnectTimeout(15000);
httpsURLConnection.setDoInput(true);
httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.connect();
int mStatus = httpsURLConnection.getResponseCode();
if (mStatus == 200)
return readResponse(httpsURLConnection.getInputStream()).toString();
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static StringBuilder readResponse(InputStream inputStream) throws IOException, NullPointerException {
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
stringBuilder.append(line);
}
return stringBuilder;
}
}
コードを更新してもよろしいですか? HttpClientは廃止され、Android 6では使用できなくなったようです。他のものを使用する必要があります。 –
私はやります... –
会社の本当のアプリに合法的にこれを使っても大丈夫ですか?私が読むべきライセンスはありますか?それともすべて無料ですか? –
。これは私のために働くあなたのlatlog で以下asyntaskを呼び出しとにかくありがとう! – Dimitris
GeoCoderは逆ジオコーディングも行います - 私は、緯度と経度を取り入れてアドレスに逆ジオコードを取り入れたgetFromLocationを使用するようにコードを更新しました。 – ianhanniballake
@ianhanniballakeが正しい場合は、 'GeoCoder'を使う必要があります。追加のコメントを追加するだけで、自分のアドレスを次のようにフォーマットしました: 'String addressText = String.format("%s、%s、%s "、 \t bestMatch.getMaxAddressLineIndex()> 0?bestMatch.getAddressLine(0):" "、 \t bestMatch.getLocality()、 \t bestMatch.getCountryName());'。これはトーストを介して単に 'addressText'値を渡すだけで、ストリートアドレスをユーザに表示することができます。 – cking24343