https://developers.google.com/maps/documentation/geocoding/
Geocoding Requests
A Geocoding API request must be of the following form:
http://maps.googleapis.com/maps/api/geocode/output?parameters
where output
may be either of the following values:
-
json
(recommended) indicates output in JavaScript Object Notation (JSON) -
xml
indicates output as XML
To access the Geocoding API over HTTPS, use:
https://maps.googleapis.com/maps/api/geocode/output?parameters
HTTPS is recommended for applications that include sensitive user data, such as a user's location, in requests.
In either case, certain parameters are required while some are optional. As is standard in URLs, all parameters are separated using the ampersand (&
) character. The list of parameters and their possible values are enumerated below.
Required parameters
-
address
— The address that you want to geocode.
orlatlng
— The textual latitude/longitude value for which you wish to obtain the closest, human-readable address. See Reverse Geocoding for more information.
orcomponents
— A component filter for which you wish to obtain a geocode. See Component Filtering for more information. The components filter will also be accepted as an optional parameter if anaddress
is provided. -
sensor
— Indicates whether or not the geocoding request comes from a device with a location sensor. This value must be eithertrue
orfalse
.
Maps API for Business users must include valid client
and signature
parameters with their Geocoding requests. Please refer to Maps API for Business Web Services for more information.
Optional parameters
-
bounds
— The bounding box of the viewport within which to bias geocode results more prominently. This parameter will only influence, not fully restrict, results from the geocoder. (For more information see Viewport Biasing below.) -
language
— The language in which to return results. See the list of supported domain languages. Note that we often update supported languages so this list may not be exhaustive. Iflanguage
is not supplied, the geocoder will attempt to use the native language of the domain from which the request is sent wherever possible. -
region
— The region code, specified as a ccTLD ("top-level domain") two-character value. This parameter will only influence, not fully restrict, results from the geocoder. (For more information see Region Biasing below.) -
components
— The component filters, separated by a pipe (|
). Each component filter consists of acomponent:value
pair and will fully restrict the results from the geocoder. For more information see Component Filtering, below.
输入:https://maps.googleapis.com/maps/api/geocode/xml?address=SFO&sensor=false
sensor=false传感器设置
查询出旧金山地址(xml格式):
输入:https://maps.googleapis.com/maps/api/geocode/jsonlatlng=113.298227,23.135032&sensor=false
查询吃该经纬度的地址(json格式):会自动下载一个json文件
bounds的作用:在指定的经纬度范围内查询
http://maps.google.com/maps/api/geocode/json?address=SFO&bounds=39.125367,118.326182|42.271635,-40.287321&sensor=false
region的作用:在给定国家代码的国家中查询(es:西班牙的简写)
http://maps.google.com/maps/api/geocode/json?address=Toledo&sensor=false®ion=es
Geocoding的返回值:
红色的地方式数组:
如何使用Gson库解析出要用的东西:
Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。可以将一个 JSON 字符串转成一个 Java 对象,或者反过来。 示例代码: Gson gson = new Gson(); |
testResult.java根据得到的json文件设计:
取得数组还是字符串:
package com.se7en;
import java.util.List;
public class TestResult {
private String status;
private List<Result> results;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
public String toString (){
return "TestResult[results: "+results+"; status:"+status+"]";
}
}
注:
当BufferedReader在读取文本文件时,会先尽量从文件中读入字符数据并置入缓冲区,而之后若使用read()方法,会先从缓冲区中进行读取。如果缓冲区数据不足,才会再从文件中读取,使用BufferedWriter时,写入的数据并不会先输出到目的地,而是先存储至缓冲区中。如果缓冲区中的数据满了,才会一次对目的地进行写出。
public interface HttpEntity
An entity that can be sent or received with an HTTP message. Entities can be found in some requests
and in responses
, where they are optional.
getContent()
Returns a content stream of the entity.
public class MainActivity extends Activity {
private Button geocodingButton = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
geocodingButton = (Button)findViewById(R.id.geoButton);
geocodingButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
//针对上一节中Geocoder服务不可用的情况,此处使用google maps中的服务替代
String url = "http://maps.google.com/maps/api/geocode/json?address=SFO&sensor=false";
//创建一个默认的HttpClient对象,向指定的url发送http请求
HttpClient client = new DefaultHttpClient();
String resonseData = "";
try {
//创建一个GET请求向指定的url发送http请求并将响应内容转换成字符串
HttpResponse response = client.execute(new HttpGet(url));
//取得服务器返回的响应
HttpEntity entity = response.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
String line = "";
while((line =br.readLine() ) != null){
resonseData = resonseData + line;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Gson gson = new Gson();
//
TestResult testResult = gson.fromJson(resonseData,TestResult.class);
System.out.println(resonseData);
System.out.println(testResult);
}
});
}