zoukankan      html  css  js  c++  java
  • ANDROID_MARS学习笔记_S03_006_geocoding、HttpClient

    一、简介

    二、代码
    1.xml
    (1)AndroidManifest.xml

    1     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    2     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    3     <uses-permission android:name="android.permission.INTERNET"/>

    2.java
    (1)MainActivity.java

     1 package com.location4;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.IOException;
     5 import java.io.InputStreamReader;
     6 
     7 import org.apache.http.HttpEntity;
     8 import org.apache.http.HttpResponse;
     9 import org.apache.http.client.ClientProtocolException;
    10 import org.apache.http.client.HttpClient;
    11 import org.apache.http.client.methods.HttpGet;
    12 import org.apache.http.impl.client.DefaultHttpClient;
    13 
    14 import android.annotation.SuppressLint;
    15 import android.app.Activity;
    16 import android.os.Bundle;
    17 import android.os.StrictMode;
    18 import android.view.View;
    19 import android.view.View.OnClickListener;
    20 import android.widget.Button;
    21 
    22 import com.google.gson.Gson;
    23 
    24 /**
    25  * 根据地址查询经纬度:
    26  * http://maps.google.com/maps/api/geocode/json?address=SFO&sensor=false
    27  *
    28  * 根据经纬度查询地址:
    29  * http://maps.google.com/maps/api/geocode/json?latlng=40.124356,-73.961472&sensor=false
    30  *
    31  * bounds的作用:在指定的经纬度范围内查询
    32  * http://maps.google.com/maps/api/geocode/json?address=SFO & bounds=39.125367,-118.326182|42.271635,-40.287321 & sensor=false
    33  *
    34  * region的作用:在给定国家代码的国家中查询(es:西班牙)
    35  * http://maps.google.com/maps/api/geocode/json?address=Toledo&sensor=false&region=es
    36  */
    37 
    38 @SuppressLint("NewApi")
    39 public class MainActivity extends Activity {
    40 
    41     private Button geocodingButton = null;
    42     
    43     @Override
    44     protected void onCreate(Bundle savedInstanceState) {
    45         super.onCreate(savedInstanceState);
    46         setContentView(R.layout.activity_main);
    47         if (android.os.Build.VERSION.SDK_INT > 9) {
    48             StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    49             StrictMode.setThreadPolicy(policy);
    50         }
    51         
    52         geocodingButton = (Button)findViewById(R.id.geoButton);
    53         geocodingButton.setOnClickListener(new OnClickListener() {
    54             @Override
    55             public void onClick(View v) {
    56                 //针对上一节中Geocoder服务不可用的情况,此处使用google maps中的服务替代
    57                 //String url = "http://maps.google.com/maps/api/geocode/json?address=SFO&sensor=false";
    58                 String url = "http://maps.google.com/maps/api/geocode/json?latlng=40.124356,-73.961472&sensor=false";
    59                 HttpClient client = new DefaultHttpClient();
    60                 StringBuilder responseData = new StringBuilder("");
    61                 try {
    62                     //向指定的url发送http请求
    63                     HttpResponse response = client.execute(new HttpGet(url));
    64                     //取得服务器返回的响应
    65                     HttpEntity entity = response.getEntity();
    66                     BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
    67                     String line = "";
    68                     while((line = reader.readLine()) != null) {
    69                         responseData.append(line);
    70                     }
    71                 } catch (ClientProtocolException e) {
    72                     e.printStackTrace();
    73                 } catch (IOException e) {
    74                     e.printStackTrace();
    75                 }
    76                 Gson gson = new Gson();
    77                 GeoResult geoResult = gson.fromJson(responseData.toString(), GeoResult.class);
    78                 System.out.println(responseData);
    79                 System.out.println(geoResult);
    80             }
    81         });
    82     }
    83 }

    (2)GeoResult.java

     1 package com.location4;
     2 
     3 import java.util.List;
     4 
     5 public class GeoResult {
     6 
     7     private String status;
     8     private List<Result> results;
     9     public String getStatus() {
    10         return status;
    11     }
    12     public void setStatus(String status) {
    13         this.status = status;
    14     }
    15     public List<Result> getResults() {
    16         return results;
    17     }
    18     public void setResults(List<Result> results) {
    19         this.results = results;
    20     }
    21     @Override
    22     public String toString() {
    23         return "GeoResult [status=" + status + ", results=" + results + "]";
    24     }
    25 }

    (3)Result.java

     1 package com.location4;
     2 
     3 import java.util.Arrays;
     4 
     5 public class Result {
     6 
     7     private String[] types;
     8     private String formatted_address;
     9     private String place_id;
    10     public String getPlace_id() {
    11         return place_id;
    12     }
    13     public void setPlace_id(String place_id) {
    14         this.place_id = place_id;
    15     }
    16     public String[] getTypes() {
    17         return types;
    18     }
    19     public void setTypes(String[] types) {
    20         this.types = types;
    21     }
    22     public String getFormatted_address() {
    23         return formatted_address;
    24     }
    25     public void setFormatted_address(String formatted_address) {
    26         this.formatted_address = formatted_address;
    27     }
    28     @Override
    29     public String toString() {
    30         return "Result [types=" + Arrays.toString(types)
    31                 + ", formatted_address=" + formatted_address + ", place_id="
    32                 + place_id + "]";
    33     }
    34 }

    (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的返回结果分别如下:

      1 {
      2    "results" : [
      3       {
      4          "address_components" : [
      5             {
      6                "long_name" : "511",
      7                "short_name" : "511",
      8                "types" : [ "street_number" ]
      9             },
     10             {
     11                "long_name" : "Ocean Avenue",
     12                "short_name" : "Ocean Ave",
     13                "types" : [ "route" ]
     14             },
     15             {
     16                "long_name" : "Sea Girt",
     17                "short_name" : "Sea Girt",
     18                "types" : [ "locality", "political" ]
     19             },
     20             {
     21                "long_name" : "Monmouth County",
     22                "short_name" : "Monmouth County",
     23                "types" : [ "administrative_area_level_2", "political" ]
     24             },
     25             {
     26                "long_name" : "New Jersey",
     27                "short_name" : "NJ",
     28                "types" : [ "administrative_area_level_1", "political" ]
     29             },
     30             {
     31                "long_name" : "美国",
     32                "short_name" : "US",
     33                "types" : [ "country", "political" ]
     34             },
     35             {
     36                "long_name" : "08750",
     37                "short_name" : "08750",
     38                "types" : [ "postal_code" ]
     39             },
     40             {
     41                "long_name" : "2712",
     42                "short_name" : "2712",
     43                "types" : [ "postal_code_suffix" ]
     44             }
     45          ],
     46          "formatted_address" : "511 Ocean Ave, Sea Girt, NJ 08750美国",
     47          "geometry" : {
     48             "location" : {
     49                "lat" : 40.131507,
     50                "lng" : -74.028998
     51             },
     52             "location_type" : "ROOFTOP",
     53             "viewport" : {
     54                "northeast" : {
     55                   "lat" : 40.1328559802915,
     56                   "lng" : -74.02764901970851
     57                },
     58                "southwest" : {
     59                   "lat" : 40.1301580197085,
     60                   "lng" : -74.03034698029151
     61                }
     62             }
     63          },
     64          "place_id" : "ChIJSUENuniIwYkRm9pgGolNa2s",
     65          "types" : [ "street_address" ]
     66       },
     67       {
     68          "address_components" : [
     69             {
     70                "long_name" : "美国",
     71                "short_name" : "US",
     72                "types" : [ "country", "political" ]
     73             }
     74          ],
     75          "formatted_address" : "美国",
     76          "geometry" : {
     77             "bounds" : {
     78                "northeast" : {
     79                   "lat" : 71.3867745,
     80                   "lng" : -66.9502861
     81                },
     82                "southwest" : {
     83                   "lat" : 18.9106768,
     84                   "lng" : 172.4458955
     85                }
     86             },
     87             "location" : {
     88                "lat" : 37.09024,
     89                "lng" : -95.712891
     90             },
     91             "location_type" : "APPROXIMATE",
     92             "viewport" : {
     93                "northeast" : {
     94                   "lat" : 49.38,
     95                   "lng" : -66.94
     96                },
     97                "southwest" : {
     98                   "lat" : 25.82,
     99                   "lng" : -124.39
    100                }
    101             }
    102          },
    103          "place_id" : "ChIJCzYy5IS16lQRQrfeQ5K5Oxw",
    104          "types" : [ "country", "political" ]
    105       }
    106    ],
    107    "status" : "OK"
    108 }
      1 <GeocodeResponse>
      2     <status>OK</status>
      3     <result>
      4         <type>street_address</type>
      5         <formatted_address>511 Ocean Ave, Sea Girt, NJ 08750美国</formatted_address>
      6         <address_component>
      7             <long_name>511</long_name>
      8             <short_name>511</short_name>
      9             <type>street_number</type>
     10         </address_component>
     11         <address_component>
     12             <long_name>Ocean Avenue</long_name>
     13             <short_name>Ocean Ave</short_name>
     14             <type>route</type>
     15         </address_component>
     16         <address_component>
     17             <long_name>Sea Girt</long_name>
     18             <short_name>Sea Girt</short_name>
     19             <type>locality</type>
     20             <type>political</type>
     21         </address_component>
     22         <address_component>
     23             <long_name>Monmouth County</long_name>
     24             <short_name>Monmouth County</short_name>
     25             <type>administrative_area_level_2</type>
     26             <type>political</type>
     27         </address_component>
     28         <address_component>
     29             <long_name>New Jersey</long_name>
     30             <short_name>NJ</short_name>
     31             <type>administrative_area_level_1</type>
     32             <type>political</type>
     33         </address_component>
     34         <address_component>
     35             <long_name>美国</long_name>
     36             <short_name>US</short_name>
     37             <type>country</type>
     38             <type>political</type>
     39         </address_component>
     40         <address_component>
     41             <long_name>08750</long_name>
     42             <short_name>08750</short_name>
     43             <type>postal_code</type>
     44         </address_component>
     45         <address_component>
     46             <long_name>2712</long_name>
     47             <short_name>2712</short_name>
     48             <type>postal_code_suffix</type>
     49         </address_component>
     50         <geometry>
     51             <location>
     52                 <lat>40.1315070</lat>
     53                 <lng>-74.0289980</lng>
     54             </location>
     55             <location_type>ROOFTOP</location_type>
     56             <viewport>
     57                 <southwest>
     58                     <lat>40.1301580</lat>
     59                     <lng>-74.0303470</lng>
     60                 </southwest>
     61                 <northeast>
     62                     <lat>40.1328560</lat>
     63                     <lng>-74.0276490</lng>
     64                 </northeast>
     65             </viewport>
     66         </geometry>
     67         <place_id>ChIJSUENuniIwYkRm9pgGolNa2s</place_id>
     68     </result>
     69     <result>
     70         <type>country</type>
     71         <type>political</type>
     72         <formatted_address>美国</formatted_address>
     73         <address_component>
     74             <long_name>美国</long_name>
     75             <short_name>US</short_name>
     76             <type>country</type>
     77             <type>political</type>
     78         </address_component>
     79         <geometry>
     80             <location>
     81                 <lat>37.0902400</lat>
     82                 <lng>-95.7128910</lng>
     83             </location>
     84             <location_type>APPROXIMATE</location_type>
     85             <viewport>
     86                 <southwest>
     87                     <lat>25.8200000</lat>
     88                     <lng>-124.3900000</lng>
     89                 </southwest>
     90                 <northeast>
     91                     <lat>49.3800000</lat>
     92                     <lng>-66.9400000</lng>
     93                 </northeast>
     94             </viewport>
     95             <bounds>
     96                 <southwest>
     97                     <lat>18.9106768</lat>
     98                     <lng>172.4458955</lng>
     99                 </southwest>
    100                 <northeast>
    101                     <lat>71.3867745</lat>
    102                     <lng>-66.9502861</lng>
    103                 </northeast>
    104             </bounds>
    105         </geometry>
    106         <place_id>ChIJCzYy5IS16lQRQrfeQ5K5Oxw</place_id>
    107     </result>
    108 </GeocodeResponse>

     

     

  • 相关阅读:
    4.JDBC技术
    3.MySQL数据库
    2.Oracle数据库
    (转载)Linux 套接字编程中的 5 个隐患
    一个Sqrt函数引发的血案
    二叉搜索树BinarySearchTree(C实现)
    vector,list.queue,array.....
    boost::Circular Buffer
    boost::operators
    Disjoint Sets
  • 原文地址:https://www.cnblogs.com/shamgod/p/5203990.html
Copyright © 2011-2022 走看看