您的位置:首页 > 编程语言 > Go语言

Google 地理信息反解析

2015-01-28 11:41 204 查看
Android 为地址反解析提供了标准的api 方案,但该方案并不是android sdk的一部分。手机用户要想
正常使用该功能,手机上必须安装 google map。但国内没有厂家默认内置google map, 手机用户也不可能
自动安装。反解析的方案国内应该可以通过baidu地图api接口,国外的话,还是的通过google.

主要研究下国外地理信息反解方式

1.获取经纬度provider 方式
gps 方式: LocationManager.GPS_PROVIDER
net 方式: LocationManager.GPS_NET

2种方式优缺点如下:
GPS_PROVIDER 通过卫星与手机定位经纬度,最小精度可以达到10m左右,但能耗比较大,在手机上不利于
长期开启。

GPS_NET 该经纬度误差较大,基本在1公里内(2g,3g),如果使用wi-fi误差会远远超过1公里。原因是该方式获取
的并不是手机本身的地理位置经纬度,而是服务站,或者isp提供商的经纬度信息。如果你用wi-fi方式,并使用国外
代理服务器上网,这个偏差可能跨越半个地球。

2. google 反解的标准方式
引用包名: android.location.Geocoder;
因不是标准api组成,需要通过Geocoder.isPresent先判别能不能使用,然后执行反解析,代码如下:

private void
getAddressFromLocation(double latitude, double longitude)
{

try

{

if (!Geocoder.isPresent())

{ //api level
9

return ;

}



Utils. printLog("GeophyInfoTracker", "getAddressFromLocation");



Geocoder geocoder = new Geocoder(s_CurUIContext ,Locale.ENGLISH);

List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);

if (addresses
!= null && addresses.size() > 0) {

Address address = addresses.get(0);



mCountryName =
address.getCountryName();// 国家

mCountryShortName =
address.getCountryCode();

mLocality =
address.getLocality();// 市

mSubLocality =
address.getSubLocality();// 区

}

}

catch (Exception
e) {

Utils. printLog("Geograhy:", "getFromLocation
exception:" + e.toString());}



}

3. google 反解的非api方式

如果Geocoder 组件不可使用,可以把经纬度直接通过网络发送google map http服务器 去获取
反解析信息:
URL form:https://maps.googleapis.com/maps/api/geocode/json?latlng=" +

strLatitude + "," + strLongitude + "&sensor=true_or_false"

把该链接通过 HttpPost 类打开,然后接收数据即可。

Google 支持2种数据返回格式 :json, xml 上面的链接返回json 格式数据。

返回数据样例:

{

"address_components" : [

{

"long_name" : "430223",

"short_name" : "430223",

"types" : [ "postal_code" ]

},

{

"long_name" : "Jiangxia",

"short_name" : "Jiangxia",

"types" : [ "sublocality_level_1", "sublocality", "political" ]

},

{

"long_name" : "Wuhan",

"short_name" : "Wuhan",

"types" : [ "locality", "political" ]

},

{

"long_name" : "Hubei",

"short_name" : "Hubei",

"types" : [ "administrative_area_level_1", "political" ]

},

{

"long_name" : "China",

"short_name" : "CN",

"types" : [ "country", "political" ]

}

],

"formatted_address" : "Jiangxia, Wuhan, Hubei, China, 430223",

"geometry" : {

"bounds" : {

"northeast" : {

"lat" : 30.4692894,

"lng" : 114.4292148

},

"southwest" : {

"lat" : 30.3985714,

"lng" : 114.3579131

}

}

对于xml格式解析的开源包很多,不再赘述,对应json提供代码如下:

private void getAddressbyGoogleMapWeb()
{
try {
final String
strUrl = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" +
mstrLatitude + "," + mstrLongitude + "&sensor=true_or_false" ;

HttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(strUrl);

HttpResponse response = httpClient.execute(httppost);
StatusLine status = response.getStatusLine();

if (status.getStatusCode()
!= 200)
{
return ;
}

String strRes = EntityUtils. toString(response.getEntity(), "UTF-8" );

JSONObject jo = new JSONObject(strRes);
if (!jo.has("results" ))
return ;

JSONArray joResult = jo.getJSONArray( "results" );
for (int i
= 0;i < joResult.length();i++)
{
JSONObject ijo = joResult.getJSONObject(i);
if (!ijo.has("types" ))
continue ;
JSONArray ijsaTypes = ijo.getJSONArray( "types" );
String strType = ijsaTypes.getString(0);
if (strType.contains("country" ))
{
if (ijo.has("address_components" ))
{
JSONArray Oaddress = ijo.getJSONArray("address_components" );
for (int j
= 0;j < Oaddress.length();j++)
{
JSONObject item = Oaddress.getJSONObject(j);
if (item.has("long_name" ))
{
Utils. printLog( "Geo",item.getString( "long_name" ));
mCountryName =
item.getString("long_name" );
}

if (item.has("short_name" ))
{
Utils. printLog( "Geo",item.getString( "short_name" ));
mCountryShortName =
item.getString("short_name" );
}
}

}
}
}

} catch (Exception
e)
{
e.printStackTrace();
}

}
google 地图的官方链接地址:https://developers.google.com/maps/documentation/geocoding/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐