Google Geocoding API(地址解析)
来源:互联网 发布:官职 知 编辑:程序博客网 时间:2024/05/19 07:07
上一片我们大概了解了一下Geocoder的bug和它的替代方法,Geocoding,直接利用http地址进行解析;下面我们来用一个实例看看如何使用http地址反回来的数据进行解析,我们这里需要用到Google自己开发的一个框架来解析json;json1.7.jar,这个类库的学习,请看:http://code.google.com/p/google-gson/
下面看实例%
1、我们如果要访问网络,首先需要在mainandroidfest.xml文件里添加权限:<uses-permission android:name="android.permission.INTERNET" />
2、修改main.xml文件,添加按钮:点击事件,进行解析;
3、给按钮添加监听器;
4、在这里我们用的是google json进行解析,所以需要两个模型,分别代表json解析出来的两个数据。一个代表一对象,list存储数组;
两具模型为:
- public class Result {
- private String[] types;
- private String formatted_address;
- public String getFormatted_address() {
- return formatted_address;
- }
- public void setFormatted_address(String formattedAddress) {
- formatted_address = formattedAddress;
- }
- public String[] getTypes() {
- return types;
- }
- public void setTypes(String[] types) {
- this.types = types;
- }
- @Override
- public String toString() {
- return "Result [formatted_address=" + formatted_address + ", types="
- + Arrays.toString(types) + "]";
- }
- }
- 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;
- }
- @Override
- public String toString() {
- return "TestResult [results=" + results + ", status=" + status + "]";
- }
- }
Activity代码为:
- public class HelloGPS2Activity extends Activity {
- private Button geoButton = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- geoButton = (Button)findViewById(R.id.geoButton);
- geoButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false";
- //创建一个HttpClient对象
- HttpClient httpClient = new DefaultHttpClient();
- String responseData = "";
- try{
- //向指定的URL发送Http请求
- HttpResponse response = httpClient.execute(new HttpGet(url));
- //取得服务器返回的响应
- HttpEntity entity = response.getEntity();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
- String line = "";
- while((line = bufferedReader.readLine()) != null){
- responseData = responseData + line;
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- Gson gson = new Gson();
- TestResult testResult = gson.fromJson(responseData, TestResult.class);
- System.out.println(testResult);
- }
- });
- }
- }
当运行之后 :点击按钮:就会将接受到的地址JSON数据解析出来:
- 08-15 03:33:23.703: INFO/System.out(281): TestResult [results=[Result [formatted_address=279-281 Bedford Ave, Brooklyn, NY 11211, USA, types=[street_address]], Result [formatted_address=Williamsburg, NY, USA, types=[neighborhood, political]], Result [formatted_address=Brooklyn, NY 11211, USA, types=[postal_code]], Result [formatted_address=Brooklyn, NY, USA, types=[sublocality, political]], Result [formatted_address=Kings, New York, USA, types=[administrative_area_level_2, political]], Result [formatted_address=New York, NY, USA, types=[locality, political]], Result [formatted_address=New York, USA, types=[administrative_area_level_1, political]]], status=OK]
我们可以进一步提取里面的信息
0 0
- Google Geocoding API(地址解析)
- Google Geocoding API(地址解析)
- 通过Google Geocoding API 解析地址
- 通过Google Geocoding API 解析地址
- 在android用Google Geocoding API服务解析地址(关键词:android/GPS/Geocoding API/getLocationFrom()返回null)
- Google Geocoding API 免费版和商业版(地理编码逆地址)
- GoogleMap Geocoding API 地址解析受限的解决办法
- 使用谷歌在线geocoding api解析地址
- 利用百度Geocoding API实现逆地址解析
- Android:利用Google GeoCoding API替代Geocoder & 解决地址语言问题 (转)
- java程序调用百度Geocoding API逆地址解析通过经纬度查询位置
- google map api 地址解析
- Google Maps API Web Services(一:The Google Geocoding API)
- google的API坐标解析地址
- google Map API实现地址解析
- mapkit中使用Google Geocoding API来进行搜索定位
- Google Geocoding API---地理编码与反向地理编码
- 百度地图Geocoding API 用法,返回json解析
- ASP.NET的工作
- 汉诺塔详解
- 写点东西
- xmpp 图片、音频
- C# 操作 XML
- Google Geocoding API(地址解析)
- ios 企业应用是否给用户用?http://mobiledan.net/2012/09/08/can-ios-enterprise-apps-be-used-by-customers/
- Spring --- java定时器,Spring定时器和Quartz定时器
- 近几年行人检测文章推荐
- 中缀转后缀
- hadoop 模拟百度网盘
- AJAX实现DropDownList无刷新二级联动
- 内部可变状态escape实例
- Tomcat的部署,服务器不能联网时,会报错(DTD,dom4j)