歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android 通過經緯度獲取地理位置信息

首先獲取LocationManager、Location及Criteria

  1. LocationManager locationManager;  
  2.                 String context = Context.LOCATION_SERVICE;  
  3.                 locationManager = (LocationManager) getSystemService(context);  
  4.                 Criteria criteria = new Criteria();  
  5.                 criteria.setAccuracy(Criteria.ACCURACY_FINE);  
  6.                 criteria.setAltitudeRequired(false);  
  7.                 criteria.setBearingRequired(false);  
  8.                 criteria.setCostAllowed(false);  
  9.                 criteria.setPowerRequirement(Criteria.POWER_LOW);  
  10.                 String provider = locationManager.getBestProvider(criteria,  
  11.                         true);  
  12.                 Location location = locationManager  
  13.                         .getLastKnownLocation(provider);  

Criteria 類介紹:

Criteria是“精確度”的意思,Criteria類提供若干個查詢條件:

(1) public void setAccuracy( int accuracy)

位置解析的精度,高或低

參數: Criteria. ACCURACY_FINE,精確模式;

Criteria. ACCURACY_COARSE,模糊模式;

(2) public void setAltitudeRequired(booleanaltitudeRequired)

是否提供海拔高度信息,是或否

(3) public void setBearingRequired(boolean bearingRequired)

是否提供方向信息,是或否

(4) public void setCostAllowed(boolean costAllowed)

是否允許運營商計費,是或否

(5) public void setPowerRequirement(int level)

電池消耗,無、低、中、高,參數 Criteria. NO_REQUIREMENT,Criteria. POWER_LOW,Criteria. POWER_MEDIUM, orCriteria. POWER_HIGH,

(6) public void setSpeedRequired(boolean speedRequired)

是否提供速度信息,是或否

獲取地理信息函數:

  1. private void updateWithNewLocation(Location location) {  
  2.         String coordinate;  
  3.         TextView loc = (TextView) findViewById(R.id.loc);  
  4.         String addressStr = "no address \n";  
  5.   
  6.         if (location != null) {  
  7.             double lat = location.getLatitude();  
  8.             double lng = location.getLongitude();  
  9.             coordinate = "Latitude:" + lat + "\nLongitude:" + lng;  
  10.             double latitude = location.getLatitude();  
  11.             double longitude = location.getLongitude();  
  12.             Geocoder geocoder = new Geocoder(context, Locale.getDefault());  
  13.             try {  
  14.                 List<Address> addresses = geocoder.getFromLocation(latitude,  
  15.                         longitude, 1);  
  16.                 StringBuilder sb = new StringBuilder();  
  17.                 if (addresses.size() > 0) {  
  18.                     Address address = addresses.get(0);  
  19.                     for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {  
  20.                         sb.append(address.getAddressLine(i)).append("\n");  
  21.                     }  
  22.                     sb.append(address.getLocality()).append("\n");  
  23.                     sb.append(address.getLocality()).append("\n");  
  24.                     sb.append(address.getCountryName());  
  25.                     addressStr = sb.toString();  
  26.                 }  
  27.             } catch (IOException e) {  
  28.                 e.printStackTrace();  
  29.             }  
  30.         } else {  
  31.             coordinate = "no coordinate!\n";  
  32.         }  
  33.         loc.setText("your coordinate:\n" + coordinate + "\n"  
  34.                 + addressStr);  
  35.     }  
Copyright © Linux教程網 All Rights Reserved