我正在使用Google Map V2 Android,地图已经在我的应用程序中显示了出来。 我想要从给定的地址获取纬度和经度。
例如:如果我在文本框中输入“Indore”,那么我会得到这个城市的纬度和经度。
我正在使用Google Map V2 Android,地图已经在我的应用程序中显示了出来。 我想要从给定的地址获取纬度和经度。
例如:如果我在文本框中输入“Indore”,那么我会得到这个城市的纬度和经度。
您可以使用此方法
public void getLatLongFromPlace(String place) {
try {
Geocoder selected_place_geocoder = new Geocoder(context);
List<Address> address;
address = selected_place_geocoder.getFromLocationName(place, 5);
if (address == null) {
d.dismiss();
} else {
Address location = address.get(0);
Latitude lat= location.getLatitude();
Longitude lng = location.getLongitude();
}
} catch (Exception e) {
e.printStackTrace();
fetchLatLongFromService fetch_latlng_from_service_abc = new fetchLatLongFromService(
place.replaceAll("\\s+", ""));
fetch_latlng_from_service_abc.execute();
}
}
//Sometimes happens that device gives location = null
public class fetchLatLongFromService extends
AsyncTask<Void, Void, StringBuilder> {
String place;
public fetchLatLongFromService(String place) {
super();
this.place = place;
}
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
this.cancel(true);
}
@Override
protected StringBuilder doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
String googleMapUrl = "http://maps.googleapis.com/maps/api/geocode/json?address="
+ this.place + "&sensor=false";
URL url = new URL(googleMapUrl);
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(
conn.getInputStream());
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
String a = "";
return jsonResults;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(StringBuilder result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
try {
JSONObject jsonObj = new JSONObject(result.toString());
JSONArray resultJsonArray = jsonObj.getJSONArray("results");
// Extract the Place descriptions from the results
// resultList = new ArrayList<String>(resultJsonArray.length());
JSONObject before_geometry_jsonObj = resultJsonArray
.getJSONObject(0);
JSONObject geometry_jsonObj = before_geometry_jsonObj
.getJSONObject("geometry");
JSONObject location_jsonObj = geometry_jsonObj
.getJSONObject("location");
String lat_helper = location_jsonObj.getString("lat");
double lat = Double.valueOf(lat_helper);
String lng_helper = location_jsonObj.getString("lng");
double lng = Double.valueOf(lng_helper);
LatLng point = new LatLng(lat, lng);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
通常查找位置的方法是我之前提到过的那个,但有时网络提供商或GPS提供商无法获取位置,因此返回空值。这也发生在我身上,但当位置为空并抛出异常时,使用谷歌网络服务获取纬度和经度。首先尝试使用上述方法获取位置,如果失败(有时会发生),则使用第二种方法从网络服务获取位置。
您好,请使用以下谷歌API完成此操作!
http://maps.google.com/maps/api/geocode/json?address=Indore&sensor=false
您可以在您的程序中使用下面给出的函数来获取纬度和经度。
public static void getLatLongFromGivenAddress(String youraddress) {
String uri = "http://maps.google.com/maps/api/geocode/json?address=" +
youraddress + "&sensor=false"
HttpGet httpGet = new HttpGet(uri);
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
lng = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
Log.d("latitude", lat);
Log.d("longitude", lng);
} catch (JSONException e) {
e.printStackTrace();
}
}
要查看此API,请访问以下链接:
http://maps.google.com/maps/api/geocode/json?address=Indore&sensor=false
您将获得以下JSON,您需要解析:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Indore",
"short_name" : "Indore",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Indore",
"short_name" : "Indore",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Madhya Pradesh",
"short_name" : "MP",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Indore, Madhya Pradesh, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 22.8485735,
"lng" : 75.965395
},
"southwest" : {
"lat" : 22.6076326,
"lng" : 75.7411195
}
},
"location" : {
"lat" : 22.7195687,
"lng" : 75.8577258
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 22.8485735,
"lng" : 75.965395
},
"southwest" : {
"lat" : 22.6076326,
"lng" : 75.7411195
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
public void getLatLongFromGivenAddress(String address)
{
double lat= 0.0, lng= 0.0;
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try
{
List<Address> addresses = geoCoder.getFromLocationName(address , 1);
if (addresses.size() > 0)
{
GeoPoint p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
lat=p.getLatitudeE6()/1E6;
lng=p.getLongitudeE6()/1E6;
Log.d("Latitude", ""+lat);
Log.d("Longitude", ""+lng);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
迄今为止,所有的答案都是有效的,可以使用地理编码API或Geocoder对象,我只想指出这一点:Google Geocoder服务不可用(坐标转地址)
我不知道这个问题发生的频率,但有时候一些设备不能使用Geocoder对象,所以我的做法是将两种方法结合起来,回退到HTTP地理编码API。
愉快的编程
只需使用以下代码,您就可以得到想要的结果:
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(ctx, Locale.getDefault());
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
其中数字1是限制获取最大结果的数量。