我跟随this tutorial获取v2 GoogleMap中心点的位置。现在我想使用反向地理编码获取该点的地址....
我在点击按钮时使用以下代码获取LatLang的中心点:
@Override
public void onClick(View view) {
if(view.getId()==R.id.btn_set_location){
VisibleRegion visibleRegion = map.getProjection()
.getVisibleRegion();
Point x = map.getProjection().toScreenLocation(
visibleRegion.farRight);
Point y = map.getProjection().toScreenLocation(
visibleRegion.nearLeft);
Point centerPoint = new Point(x.x / 2, y.y / 2);
LatLng centerFromPoint = map.getProjection().fromScreenLocation(
centerPoint);
}
}
我已经看到其他的tutotials在我的情况下单独使用纬度和经度来获取LatLang,如何使用LatLang来获取地址...这里需要帮助....如果有其他方法可以获得中心点地图和同一点的地址...任何形式的帮助将不胜感激...谢谢
答案 0 :(得分:0)
public class AsyncRouteGetter extends AsyncTask<Double, String, String> {
protected String doInBackground(Double... coords) {
String xml = "";
String url = "http://maps.google.com/maps/api/geocode/xml?address=" + coords[0] + "," + coords[1] + "&sensor=false";
//String url = "http://maps.google.com/maps/api/geocode/json?address=51.0031761,17.0499418&sensor=false";
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (Exception e) { e.printStackTrace(); }
//Log.d("LOK", xml);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document doc = null;
try {
builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
doc = builder.parse(is);
} catch (Exception e) { Log.d("DOC", "BLAD2"); e.printStackTrace(); return null; }
String result = doc.getElementsByTagName("formatted_address").item(0).getTextContent();
Log.d("LOKALIZACJA", result);
return result;
}
}
这对我来说非常适合。随意将此标记为答案:)
<强>更新强>
AsyncRouteGetter arg = new AsyncRouteGetter();
arg.execute(location.latitude, location.longitude);
String route = null;
try {
route = arg.get();
} catch (Exception e) { Log.d("LOK", "Error1"); }
if (route != null) {
Log.d("LOK", route);
currentAddress = route;
} else {
Log.d("LOK", "Error2");
currentAddress = "anything, so you wont work with null";
}
答案 1 :(得分:0)
我想,你想把LatLang作为参数传递。我用以下代码解决了这个问题:
$("#km").keyup(function () {
var price = Number($(this).val());
var total = (price) * 0.25;
$("#amount").val(total);
});
希望这对你有用&gt;&gt;&gt;
编辑后:
您可以关注this,如果您正在处理活动类,如果您正在使用Fragment类,则以下代码将适用于您:
private class ReverseGeoCoding extends AsyncTask<LatLng, Void, String> {
Context mContext;
public ReverseGeoCoding(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);
} catch (IOException e) {
e.printStackTrace();
}
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());
}
return addressText;
}