最近在做一个android应用,需要粗略定位获取用户所在城市,利用GPS获得坐标后,调用google的反向地址解析接口
String path = "http://maps.googleapis.com/maps/api/geocode/json?latlng=34.415973384481866,109.599609375&sensor=true";便可得到一个json数据
{
"results" : [
{
"address_components" : [
{
"long_name" : "Xisan Rd",
"short_name" : "Y398",
"types" : [ "route" ]
},
{
"long_name" : "Linwei",
"short_name" : "Linwei",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Weinan",
"short_name" : "Weinan",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Shaanxi",
"short_name" : "Shaanxi",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "China",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Xisan Rd, Linwei, Weinan, Shaanxi, China",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 34.43291650,
"lng" : 109.60018280
},
"southwest" : {
"lat" : 34.42992760,
"lng" : 109.57636530
}
},
"location" : {
"lat" : 34.43142240,
"lng" : 109.58825590
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 34.43291650,
"lng" : 109.60018280
},
"southwest" : {
"lat" : 34.42992760,
"lng" : 109.57636530
}
}
},
"types" : [ "route" ]
},
{
"address_components" : [
{
"long_name" : "Linwei",
"short_name" : "Linwei",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Weinan",
"short_name" : "Weinan",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Shaanxi",
"short_name" : "Shaanxi",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "China",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Linwei, Weinan, Shaanxi, China",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 34.79253820,
"lng" : 109.76599990
},
"southwest" : {
"lat" : 34.23814540,
"lng" : 109.38916780
}
},
"location" : {
"lat" : 34.4981920,
"lng" : 109.4927260
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 34.79253820,
"lng" : 109.76599990
},
"southwest" : {
"lat" : 34.23814540,
"lng" : 109.38916780
}
}
},
"types" : [ "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Weinan",
"short_name" : "Weinan",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Shaanxi",
"short_name" : "Shaanxi",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "China",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Weinan, Shaanxi, China",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.85415640,
"lng" : 110.60933170
},
"southwest" : {
"lat" : 34.20975990,
"lng" : 108.96733540
}
},
"location" : {
"lat" : 34.4999950,
"lng" : 109.5097860
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 34.5805260,
"lng" : 109.61952210
},
"southwest" : {
"lat" : 34.40507940,
"lng" : 109.35104370
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Shaanxi",
"short_name" : "Shaanxi",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "China",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Shaanxi, China",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 39.58701050,
"lng" : 111.24805440
},
"southwest" : {
"lat" : 31.70505980,
"lng" : 105.4919490
}
},
"location" : {
"lat" : 34.2654720,
"lng" : 108.9542390
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 39.58701050,
"lng" : 111.24805440
},
"southwest" : {
"lat" : 31.70505980,
"lng" : 105.4919490
}
}
},
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "China",
"short_name" : "CN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "China",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 53.56097399999999,
"lng" : 134.77280990
},
"southwest" : {
"lat" : 18.15352160,
"lng" : 73.49941360
}
},
"location" : {
"lat" : 35.861660,
"lng" : 104.1953970
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 53.56097399999999,
"lng" : 134.77280990
},
"southwest" : {
"lat" : 18.15352160,
"lng" : 73.49941360
}
}
},
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
我的目的是解析其中的WeiNan(渭南)。
一步一步来
1、创建android项目(我的版本是2.2.3),取名为GetLocation;
2、布局
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/latID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/lat" />
<EditText
android:id="@+id/lngID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/lng" />
</LinearLayout>
<Button
android:id="@+id/buutonID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="@string/showButton" />
<TextView
android:id="@+id/positionID"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
3、编写业务类
在业务类进行json数据的解析,在android应用中利用json与服务器进行数据交互效率比XML快,因为很多时候解析json不需要遍历整个数据。
json中用大括号{}包含的数据是json对象:
json中用中括号[]包含的数据是json数组:
可以看到,在业务类中,反反复复获取json对象和json数组,最终解析到我们想要的数据。
Geocoding.java:
public class Geocoding {
public static String getJsonData(String lat,String lng) throws Exception {
//请求路径
String path = "http://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5 * 1000);//设置超时时间
conn.setRequestMethod("GET");//设置请求方式
InputStream instream = conn.getInputStream();//获取google服务器返回的数据流
byte[] byteData = readInputStream(instream);//读取输入流
String locality = new String();
String jsonString = new String(byteData);
JSONObject jsobj1 = new JSONObject(jsonString); //获取Json对象
JSONArray array1 = jsobj1.getJSONArray("results"); //获取Json数组
JSONObject jsobj2 = array1.getJSONObject(0); //再获取Json对象
JSONArray array2 = jsobj2.getJSONArray("address_components");//再获取Json数组
for (int i = 0; i < array2.length(); i++) {
JSONObject jsobjFor = array2.getJSONObject(i);//获取对象
String types = jsobjFor.getString("types");//获取值
if ("[\"locality\",\"political\"]".equals(types)) {//检验是否匹配
locality = jsobjFor.getString("long_name");
}
}
return locality;
}
/*
* 流数据读取
*/
private static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer)) !=-1 ){
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
return outSteam.toByteArray();
}
}
5、修改activity
MainActivity.java:
6、在Manifestxml中添加访问网络的权限public class MainActivity extends Activity { EditText latEditText; EditText lngEditText; Button showButton; TextView positonTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); latEditText = (EditText)this.findViewById(R.id.latID); lngEditText = (EditText)this.findViewById(R.id.lngID); positonTextView = (TextView)this.findViewById(R.id.positionID); showButton = (Button)this.findViewById(R.id.buutonID); showButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { String lat = latEditText.getText().toString(); String lng = lngEditText.getText().toString(); try{ String position = Geocoding.getJsonData(lat,lng); positonTextView.setText("你所在的位置为:"+position); }catch(Exception e){ Log.e("MyTag",e.toString()); } } }); } }
<uses-permission android:name="android.permission.INTERNET" > </uses-permission>