我正在使用谷歌地图v2 android和地图也显示在我的应用程序中。 我只想从给定的地址获得经度和纬度。
示例:在文本字段中,如果我键入" Indore",那么我将获得该城市的纬度和经度。
答案 0 :(得分:11)
您可以使用此方法
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提供商无法获取位置因此给出空值,它也发生在我身上但当位置为空时抛出异常,使用google web服务获取lat和long。首先尝试从上面的方法获取,如果失败,有时会使用第二种方法从Web服务获取它。
答案 1 :(得分:4)
嗨,请使用以下谷歌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();
}
}
}
答案 2 :(得分:0)
到目前为止,所有答案似乎都是有效的,使用地理编码API或Geocoder对象,只是想指出这一点:Google Geocoder service is unavaliable (Coordinates to address)
我不知道这个问题多么频繁发生但是有时候某些设备无法使用Geocoder对象,所以我所做的就是将这两种方法结合起来,回到HTTP地理编码API。
快乐编码
答案 3 :(得分:-1)
只需使用以下代码即可获得所需内容:
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(ctx, Locale.getDefault());
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
其中1是获得maxResults的限制。