ANDROID_MARS学习笔记_S03_006_geocoding、HttpClient
一、简介
二、代码
1.xml
(1)AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
2.java
(1)MainActivity.java
package com.location4; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient; import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; import com.google.gson.Gson; /**
* 根据地址查询经纬度:
* http://maps.google.com/maps/api/geocode/json?address=SFO&sensor=false
*
* 根据经纬度查询地址:
* http://maps.google.com/maps/api/geocode/json?latlng=40.124356,-73.961472&sensor=false
*
* 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
*/ @SuppressLint("NewApi")
public class MainActivity extends Activity { private Button geocodingButton = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
} geocodingButton = (Button)findViewById(R.id.geoButton);
geocodingButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//针对上一节中Geocoder服务不可用的情况,此处使用google maps中的服务替代
//String url = "http://maps.google.com/maps/api/geocode/json?address=SFO&sensor=false";
String url = "http://maps.google.com/maps/api/geocode/json?latlng=40.124356,-73.961472&sensor=false";
HttpClient client = new DefaultHttpClient();
StringBuilder responseData = new StringBuilder("");
try {
//向指定的url发送http请求
HttpResponse response = client.execute(new HttpGet(url));
//取得服务器返回的响应
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
String line = "";
while((line = reader.readLine()) != null) {
responseData.append(line);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Gson gson = new Gson();
GeoResult geoResult = gson.fromJson(responseData.toString(), GeoResult.class);
System.out.println(responseData);
System.out.println(geoResult);
}
});
}
}
(2)GeoResult.java
package com.location4; import java.util.List; public class GeoResult { 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 "GeoResult [status=" + status + ", results=" + results + "]";
}
}
(3)Result.java
package com.location4; import java.util.Arrays; public class Result { private String[] types;
private String formatted_address;
private String place_id;
public String getPlace_id() {
return place_id;
}
public void setPlace_id(String place_id) {
this.place_id = place_id;
}
public String[] getTypes() {
return types;
}
public void setTypes(String[] types) {
this.types = types;
}
public String getFormatted_address() {
return formatted_address;
}
public void setFormatted_address(String formatted_address) {
this.formatted_address = formatted_address;
}
@Override
public String toString() {
return "Result [types=" + Arrays.toString(types)
+ ", formatted_address=" + formatted_address + ", place_id="
+ place_id + "]";
}
}
(4)http://maps.google.com/maps/api/geocode/json?latlng=40.124356,-73.961472&sensor=false
和http://maps.google.com/maps/api/geocode/xml?latlng=40.124356,-73.961472&sensor=false的返回结果分别如下:
{
"results" : [
{
"address_components" : [
{
"long_name" : "511",
"short_name" : "511",
"types" : [ "street_number" ]
},
{
"long_name" : "Ocean Avenue",
"short_name" : "Ocean Ave",
"types" : [ "route" ]
},
{
"long_name" : "Sea Girt",
"short_name" : "Sea Girt",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Monmouth County",
"short_name" : "Monmouth County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New Jersey",
"short_name" : "NJ",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "美国",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "08750",
"short_name" : "08750",
"types" : [ "postal_code" ]
},
{
"long_name" : "2712",
"short_name" : "2712",
"types" : [ "postal_code_suffix" ]
}
],
"formatted_address" : "511 Ocean Ave, Sea Girt, NJ 08750美国",
"geometry" : {
"location" : {
"lat" : 40.131507,
"lng" : -74.028998
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 40.1328559802915,
"lng" : -74.02764901970851
},
"southwest" : {
"lat" : 40.1301580197085,
"lng" : -74.03034698029151
}
}
},
"place_id" : "ChIJSUENuniIwYkRm9pgGolNa2s",
"types" : [ "street_address" ]
},
{
"address_components" : [
{
"long_name" : "美国",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "美国",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 71.3867745,
"lng" : -66.9502861
},
"southwest" : {
"lat" : 18.9106768,
"lng" : 172.4458955
}
},
"location" : {
"lat" : 37.09024,
"lng" : -95.712891
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 49.38,
"lng" : -66.94
},
"southwest" : {
"lat" : 25.82,
"lng" : -124.39
}
}
},
"place_id" : "ChIJCzYy5IS16lQRQrfeQ5K5Oxw",
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
<GeocodeResponse>
<status>OK</status>
<result>
<type>street_address</type>
<formatted_address>511 Ocean Ave, Sea Girt, NJ 08750美国</formatted_address>
<address_component>
<long_name>511</long_name>
<short_name>511</short_name>
<type>street_number</type>
</address_component>
<address_component>
<long_name>Ocean Avenue</long_name>
<short_name>Ocean Ave</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Sea Girt</long_name>
<short_name>Sea Girt</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Monmouth County</long_name>
<short_name>Monmouth County</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>New Jersey</long_name>
<short_name>NJ</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>美国</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>08750</long_name>
<short_name>08750</short_name>
<type>postal_code</type>
</address_component>
<address_component>
<long_name>2712</long_name>
<short_name>2712</short_name>
<type>postal_code_suffix</type>
</address_component>
<geometry>
<location>
<lat>40.1315070</lat>
<lng>-74.0289980</lng>
</location>
<location_type>ROOFTOP</location_type>
<viewport>
<southwest>
<lat>40.1301580</lat>
<lng>-74.0303470</lng>
</southwest>
<northeast>
<lat>40.1328560</lat>
<lng>-74.0276490</lng>
</northeast>
</viewport>
</geometry>
<place_id>ChIJSUENuniIwYkRm9pgGolNa2s</place_id>
</result>
<result>
<type>country</type>
<type>political</type>
<formatted_address>美国</formatted_address>
<address_component>
<long_name>美国</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>37.0902400</lat>
<lng>-95.7128910</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>25.8200000</lat>
<lng>-124.3900000</lng>
</southwest>
<northeast>
<lat>49.3800000</lat>
<lng>-66.9400000</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>18.9106768</lat>
<lng>172.4458955</lng>
</southwest>
<northeast>
<lat>71.3867745</lat>
<lng>-66.9502861</lng>
</northeast>
</bounds>
</geometry>
<place_id>ChIJCzYy5IS16lQRQrfeQ5K5Oxw</place_id>
</result>
</GeocodeResponse>
ANDROID_MARS学习笔记_S03_006_geocoding、HttpClient的更多相关文章
- Android学习笔记之HttpClient实现Http请求....
PS:最近光忙着考试了....破组成原理都看吐了....搞的什么也不想干...写篇博客爽爽吧....貌似明天就考试了...sad... 学习笔记: 1.如何实现Http请求来实现通信.... 2.解决 ...
- ANDROID_MARS学习笔记_S04_007_从服务器获取微博数据时间线
一.代码 1.xml(1)activity_main.xml <?xml version="1.0" encoding="utf-8"?> < ...
- ANDROID_MARS学习笔记_S04_005_用sing-post向腾讯微博发一条信息
一.代码流程 1.组织好sign-post需要的token,secrect 2.组织好发微博需要的信息 3.用sign-post进行签名 4.把签名结果从header中拿出来,转成entity,用ht ...
- ANDROID_MARS学习笔记_S04_004_用HTTPCLENT发带参数的get和post请求
一.代码 1.xml(1)activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...
- ANDROID_MARS学习笔记_S04_003_用HttpClent发http请求
一.代码 1.xml(1)activity_main.xml <TextView android:layout_width="wrap_content" android:la ...
- ANDROID_MARS学习笔记_S01_012_RatingBar
1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...
- ANDROID_MARS学习笔记_S01_012_SeekBar
1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...
- ANDROID_MARS学习笔记_S01_011ProgressBar
文档是这样来设置样式 <ProgressBar android:layout_width="wrap_content" android:layout_height=" ...
- ANDROID_MARS学习笔记_S01_010日期时间控件
1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...
随机推荐
- 推荐几个对Asp.Net开发者比较实用的工具
推荐几个对Asp.Net开发者比较实用的工具.大家有相关工具也可以在评论区留言,一起努力学习. 工具 1.Visual stdio Productivity Power tool:visual std ...
- css placeholder 颜色设置
因为每个浏览器的CSS选择器都有所差异,所以需要针对每个浏览器做单独的设定(可以在冒号前面写input和textarea). ::-webkit-input-placeholder { /* WebK ...
- 【JAVA错误笔记】 - c3p0问题java.lang.NoClassDefFoundError:com.mchange.v2.ser.Indirector
错误描述:java.lang.NoClassDefFoundError:com.mchange.v2.ser.Indirector 原因分析: 这是c3p0的一个错误信息,我们在下载 c3p0时候,z ...
- 解构控制反转(IoC)和依赖注入(DI)
1.控制反转 控制反转(Inversion of Control,IoC),简言之就是代码的控制器交由系统控制,而不是在代码内部,通过IoC,消除组件或者模块间的直接依赖,使得软件系统的开发更具柔性和 ...
- JavaScript高级程序设计(一):JavaScript简介
一.JavaScript实现 1.一个完整的JavaScript包含三个部分组成: 1)ECMAScript 核心 2)DOM文档对象模型 3)BOM浏览器对象模型 2.文档对象模型(DOM) 文档对 ...
- OC - 8.Quartz2D核心要点
简介 作用 绘制 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成PDF 截图\裁剪图片 自定义UI控件(通常为内部结构较复杂的控件) UIKit中的绝大部分控 ...
- 05_Excel操作_02_模拟Web环境的User列表导出
[思路解释] 在正式上到WebProject之前,准备模拟一下WebProject后台的导出流程. 主要都写在ExcelService层,在Excel的Service层,首先要获得UserList,即 ...
- 暑假集训(1)第八弹 -----简单迷宫(Poj3984)
Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...
- MySQl索引创建
一.什么是索引? 索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时MySQL必须从第一个记录开始扫描整个表的所有记录,直至找到符合要求的记录.表 ...
- 现代密码学应用的范例-PGP
PGP(Pretty Good Privacy),是一个基于RSA公钥加密体系的邮件加密软件. 产生背景: 电子邮件在传输中使用SMTP协议存在这样的问题 1.无法保证邮件在传输过程中不被人偷看 2. ...