zoukankan      html  css  js  c++  java
  • Android Google Maps API 网络服务用于网络定位、计算路线、获取经纬度、获取详细地址等

    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("北京","上海");  

      

      

  • 相关阅读:
    python面试题
    面试总结
    552 Student Attendance Record II 学生出勤记录 II
    551 Student Attendance Record I 学生出勤纪录 I
    547 Friend Circles 朋友圈
    546 Remove Boxes 移除盒子
    543 Diameter of Binary Tree 二叉树的直径
    542 01 Matrix 01 矩阵
    3.1 特性
    2.6 datetime 模块
  • 原文地址:https://www.cnblogs.com/niray/p/4681472.html
Copyright © 2011-2022 走看看