Android Google Maps API 網路服務用於網路定位、計算路線、擷取經緯度、擷取詳細地址等

來源:互聯網
上載者:User

標籤:

---恢複內容開始---

extends:http://blog.csdn.net/h7870181/article/details/12505883

 

Google Maps API 網路服務  

官網地址 :

https://developers.google.com/maps/documentation/webservices/?hl=zh-cn

 

其實就是一些介面,供我們調用,如:

1、根據地址擷取經緯度

http://maps.google.com/maps/api/geocode/json?address=北京&language=zh-CN&sensor=false

2、計算路線資料

http://maps.googleapis.com/maps/api/directions/json?origin=北京&destination=上海&sensor=false&mode=driving

3、根據經緯度擷取詳細地址

http://maps.google.com/maps/api/geocode/json?latlng="latlng"&language=zh-CN&sensor=false

等等還有很多,大家可以自己去找找

 

給大家介紹一下如果利用這些介面

實現網路定位:

首先擷取經緯度

 

 /**  * 擷取本地  * @param context  * @return  */  public String getLocation(Context context){      LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);         // 返回所有已知的位置提供者的名稱列表,包括未獲准訪問或調用活動目前已停用的。         //List<String> lp = lm.getAllProviders();         Criteria criteria = new Criteria();           criteria.setCostAllowed(false);          //設定位置服務免費          criteria.setAccuracy(Criteria.ACCURACY_COARSE); //設定水平位置精度          //getBestProvider 只有允許訪問調用活動的位置供應商將被返回         String  providerName = lm.getBestProvider(criteria, true);           if (providerName != null)         {                     Location location = lm.getLastKnownLocation(providerName);              if(location!=null){              //擷取維度資訊              double latitude = location.getLatitude();              //擷取經度資訊              double longitude = location.getLongitude();              return latitude+","+longitude;             }         }          return "";   }  

 

調用API,我這裡寫了一個工具類

 

  

package com.techrare.utils;    import java.io.IOException;  import java.io.UnsupportedEncodingException;  import java.util.ArrayList;  import java.util.List;  import java.util.Map;    import org.apache.http.HttpResponse;  import org.apache.http.NameValuePair;  import org.apache.http.ParseException;  import org.apache.http.client.ClientProtocolException;  import org.apache.http.client.HttpClient;  import org.apache.http.client.entity.UrlEncodedFormEntity;  import org.apache.http.client.methods.HttpPost;  import org.apache.http.impl.client.DefaultHttpClient;  import org.apache.http.message.BasicNameValuePair;  import org.apache.http.protocol.HTTP;  import org.apache.http.util.EntityUtils;    public class MapsApiUtils {      private static MapsApiUtils mapsApiUtils = new MapsApiUtils();            /**      * 單例模式      *       * @return      */      synchronized public static MapsApiUtils getInstance() {          return mapsApiUtils;      }            /**      * 根據API地址和參數擷取響應對象HttpResponse      *       * @param params      * @param url      * @return      */      private HttpResponse post(Map<String, Object> params, String url) {            HttpClient client = new DefaultHttpClient();          HttpPost httpPost = new HttpPost(url);          httpPost.addHeader("charset", HTTP.UTF_8);          httpPost.setHeader("Content-Type",                  "application/x-www-form-urlencoded; charset=utf-8");          HttpResponse response = null;          if (params != null && params.size() > 0) {              List<NameValuePair> nameValuepairs = new ArrayList<NameValuePair>();              for (String key : params.keySet()) {                  nameValuepairs.add(new BasicNameValuePair(key, (String) params                          .get(key)));              }              try {                  httpPost.setEntity(new UrlEncodedFormEntity(nameValuepairs,                          HTTP.UTF_8));                  response = client.execute(httpPost);              } catch (UnsupportedEncodingException e) {                  e.printStackTrace();              } catch (ClientProtocolException e) {                  e.printStackTrace();              } catch (IOException e) {                  e.printStackTrace();              } catch (RuntimeException e) {                  e.printStackTrace();              }          } else {              try {                  response = client.execute(httpPost);              } catch (ClientProtocolException e) {                  e.printStackTrace();              } catch (IOException e) {                  e.printStackTrace();              }          }          return response;      }            /**      * 得到JSON值      *       * @param params      * @param url      * @return      */      private Object getValues(Map<String, Object> params, String url) {          String token = "";          HttpResponse response = post(params, url);          if (response != null) {              try {                  token = EntityUtils.toString(response.getEntity());                  response.removeHeaders("operator");              } catch (ParseException e) {                  e.printStackTrace();              } catch (IOException e) {                  e.printStackTrace();              }          }          return token;      }            /**      * 根據google API 擷取兩地的路線      * @param origin 起點      * @param destination 終點      * @param mode 出行方式 driving駕車,  walking步行, bicycling單車, transit公交車      * @param sensor 是否來自裝有定位感應器的裝置  true Or false      * @return      */      public Object getRoutes(String origin, String destination) {          String url = "http://maps.googleapis.com/maps/api/directions/json?origin="+ origin +"&" +                  "destination="+ destination +"&sensor=false&mode=driving&region=zh";          return getValues(null, url);      }            /**      * 根據經緯度 擷取地理位置      * LatLng 經緯度以逗號隔開  緯度,經度      * @return      */      public Object getAddress(String latlng) {          String url = "http://maps.google.com/maps/api/geocode/json?latlng="+                  latlng+"&language=zh-CN&sensor=false";          return getValues(null, url);      }            /**      * 根據地址擷取經緯度      * @return      */      public Object getLatlng(String str) {          String url = "http://maps.google.com/maps/api/geocode/json?address="+                  str+"&language=zh-CN&sensor=false";          return getValues(null, url);      }  }  

 

 

調用getAddress()方法 傳遞經緯度來擷取詳細地址 返回的是JSON字串,大家解析一下就可以

 

 
MapsApiUtils.getInstance().getAddress(getLocation(context));  

 

 

 

計算路線資料 

可以得到起點到終點的時間和路程

調用getRoutes() 方法,傳起點和終點

  

MapsApiUtils.getInstance().getLatLng("39.90403,116.407526");  

 

 

 

 

根據地址擷取經緯度

  

 
MapsApiUtils.getInstance().getRoutes("北京","上海");  

  

  

Android Google Maps API 網路服務用於網路定位、計算路線、擷取經緯度、擷取詳細地址等

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.