Advertisement

android:获取GPS卫星定位 ;

阅读量:

以下是获取GPS卫星定位基础代码

复制代码
 //首先获取位置管理器

    
  
    
   LocationManager alm = (LocationManager) this
    
             .getSystemService(Context.LOCATION_SERVICE);
    
  
    
  
    
 //设置过滤条件
    
  
    
 Criteria criteria = new Criteria();
    
     criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
    
     criteria.setAltitudeRequired(false);
    
     criteria.setBearingRequired(false);
    
     criteria.setCostAllowed(true);
    
     criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
    
  
    
  
    
 String provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息
    
     Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
    
  
    
 // 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米        locationManager.requestLocationUpdates(provider, 100 * 1000, 500,                locationListener);
    
  
    
  
    
 //更新位置信息
    
 if (location != null) {            double  latitude = location.getLatitude();            double longitude= location.getLongitude();            tv1.setText("维度:" +  latitude+ "\n经度" + longitude);        } else {            tv1.setText("无法获取地理信息");        }
    
    
    
    
    代码解读

但有些权限须要配置:如下所示

复制代码
 <uses-permission android:name="android.permission.INTERNET" />  
    
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    
    
 <uses-permission android:name="android.permission.ACCESS_FIND_LOCATION" />   
    
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>  
    
    
    
    
    代码解读

但是有的时候须要将汉语描述位置信息转化成经纬度:

复制代码
 /** * 由详细地址信息转换为经纬度
    
 */
    
 public static double[] getLocationInfoByGoogle(String address){
    
 //定义一个HttpClient,用于向指定地址发送请求
    
 HttpClient client = new DefaultHttpClient();
    
  
    
 //向指定地址发送Get请求
    
 HttpGet hhtpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address="+address+"ka&sensor=false");
    
  
    
 StringBuilder sb = new StringBuilder();
    
  
    
  
    
 try {
    
 //获取服务器响应
    
 HttpResponse response = client.execute(hhtpGet);
    
  
    
 HttpEntity entity = response.getEntity();
    
  
    
 //获取服务器响应的输入流
    
 InputStream stream = entity.getContent();
    
  
    
 int b;
    
 //循环读取服务器响应
    
 while((b = stream.read()) != -1){
    
 sb.append((char)b);
    
 }
    
  
    
 //将服务器返回的字符串转换为JSONObject  对象
    
 JSONObject jsonObject = new JSONObject(sb.toString());
    
  
    
 //从JSONObject 中取出location 属性
    
 JSONObject location = jsonObject.getJSONObject("results").getJSONObject("0").getJSONObject("geometry").getJSONObject("location");
    
  
    
 //获取经度信息
    
 double longtitude = location.getDouble("lng");
    
 double latitude = location.getDouble("lat");
    
  
    
 return new double[]{longtitude,latitude};
    
  
    
 } catch (ClientProtocolException e) {
    
 e.printStackTrace();
    
 } catch (IOException e) {
    
 e.printStackTrace();
    
 } catch (JSONException e) { 
    
 e.printStackTrace();
    
 }
    
  
    
  
    
 return null;
    
 }
    
    
    
    
    代码解读

下面是将经纬度转化成汉语描述信息:

复制代码
 /** * 根据经纬度获取对应地址,此处的经纬度须使用Google或者高德地图的经纬度;
    
 */
    
  
    
 public static String getAddressByGoogle(double longitude,double latitude){
    
 //定义一个HttpClient,用于向指定地址发送请求
    
 HttpClient client = new DefaultHttpClient();
    
  
    
 //向指定地址发送Get请求
    
 HttpGet hhtpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+latitude+","+longitude+"&sensor=false®ion=cn");
    
  
    
 StringBuilder sb = new StringBuilder();
    
  
    
  
    
 try {
    
 //获取服务器响应
    
 HttpResponse response = client.execute(hhtpGet);
    
  
    
 HttpEntity entity = response.getEntity();
    
  
    
 //获取服务器响应的输入流
    
 InputStream stream = entity.getContent();
    
  
    
 int b;
    
 //循环读取服务器响应
    
 while((b = stream.read()) != -1){
    
 sb.append((char)b);
    
 }
    
  
    
 //将服务器返回的字符串转换为JSONObject  对象
    
 JSONObject jsonObject = new JSONObject(sb.toString());
    
  
    
 //从JSONObject 中取出location 属性
    
 JSONObject location = jsonObject.getJSONObject("results").getJSONObject("0").getJSONObject("formatted_address");
    
  
    
  
    
  
    
 return  location.toString();
    
  
    
 } catch (ClientProtocolException e) {
    
 e.printStackTrace();
    
 } catch (IOException e) {
    
 e.printStackTrace();
    
 } catch (JSONException e) { 
    
 e.printStackTrace();
    
 }
    
 return null;
    
  
    
  
    
 }
    
    
    
    
    代码解读

以上均使用google的api,如果大家有其他的开源api希望补充;

全部评论 (0)

还没有任何评论哟~