public class LocationUtils {//public static String cityName = "深圳";  //城市名public static String cityName ;  //城市名private static Geocoder geocoder;//此对象能通过经纬度来获取相应的城市等信息/** * 通过地理坐标获取城市名其中CN分别是city和name的首字母缩写 * @param context */public static void getCNBylocation(Context context){geocoder = new Geocoder(context);//用于获取Location对象,以及其他LocationManager locationManager; String serviceName = Context.LOCATION_SERVICE;//实例化一个LocationManager对象locationManager = (LocationManager)context.getSystemService(serviceName);//provider的类型String provider = LocationManager.NETWORK_PROVIDER;Criteria criteria = new Criteria();criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度criteria.setAltitudeRequired(false);//不要求海拔criteria.setBearingRequired(false);//不要求方位criteria.setCostAllowed(false);//不允许有话费criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗//通过最后一次的地理位置来获得Location对象Location location = locationManager.getLastKnownLocation(provider);String queryed_name = updateWithNewLocation(location);if((queryed_name != null) && (0 != queryed_name.length())){cityName = queryed_name;}/* * 第二个参数表示更新的周期,单位为毫秒;第三个参数的含义表示最小距离间隔,单位是米 * 设定每30秒进行一次自动定位 */locationManager.requestLocationUpdates(provider, 30000, 50,locationListener);//移除监听器,在只有一个widget的时候,这个还是适用的locationManager.removeUpdates(locationListener);}/** * 方位改变时触发,进行调用 */private final static LocationListener locationListener = new LocationListener() {String tempCityName;public void onLocationChanged(Location location) {tempCityName = updateWithNewLocation(location);if((tempCityName != null) && (tempCityName.length() != 0)){cityName = tempCityName;}}public void onProviderDisabled(String provider) {tempCityName = updateWithNewLocation(null);if ((tempCityName != null) && (tempCityName.length() != 0)) {cityName = tempCityName;}}public void onProviderEnabled(String provider) {}public void onStatusChanged(String provider, int status, Bundle extras) {}};/** * 更新location * @param location * @return cityName */private static String updateWithNewLocation(Location location) {String mcityName = "";double lat = 0;    double lng = 0;    List addList = null;if (location != null) {lat = location.getLatitude();lng = location.getLongitude();} else {System.out.println("无法获取地理信息");} try {addList = geocoder.getFromLocation(lat, lng, 1);//解析经纬度} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}if (addList != null && addList.size() > 0) {for (int i = 0; i < addList.size(); i++) {Address add = addList.get(i);mcityName += add.getLocality();}}if(mcityName.length()!=0){return mcityName.substring(0, (mcityName.length()-1));} else {return mcityName;}}/** * 通过经纬度获取地址信息的另一种方法 * @param latitude * @param longitude * @return 城市名 */public static String GetAddr(String latitude, String longitude) {  String addr = "";  /* * 也可以是http://maps.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s,不过解析出来的是英文地址 * 密钥可以随便写一个key=abc * output=csv,也可以是xml或json,不过使用csv返回的数据最简洁方便解析     */String url = String.format(      "http://ditu.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s",      latitude, longitude);  URL myURL = null;  URLConnection httpsConn = null;  try {  myURL = new URL(url);  } catch (MalformedURLException e) {    e.printStackTrace();    return null;  }  try {  httpsConn = (URLConnection) myURL.openConnection();      if (httpsConn != null) {  InputStreamReader insr = new InputStreamReader(  httpsConn.getInputStream(), "UTF-8");  BufferedReader br = new BufferedReader(insr);  String data = null;  if ((data = br.readLine()) != null) {  String[] retList = data.split(",");  if (retList.length > 2 && ("200".equals(retList[0]))) {  addr = retList[2];  } else {  addr = "";  }  }  insr.close();  }  } catch (IOException e) {  e.printStackTrace();     return null;  }     return addr;  }}


加上权限:





更多相关文章

  1. Android期末项目(一)—— 解析二维数组对象
  2. Android Intent传递对象
  3. Android JNI cpp文件创建 java bean 对象并返回
  4. Android高手进阶教程(十六)---Android中Intent传递对象的两种方
  5. Android上在两个Activity之间传递Bitmap对象
  6. [置顶] Android 调用js,传对象到js里面使用addJavascriptInterfac

随机推荐

  1. No cached version of androidx.appcompa
  2. android 的AIDL学习
  3. Android手势密码LockPatternView、LockPa
  4. Android(安卓)蓝牙(概述)
  5. 通用(任何android机型)Root教程(完整版!
  6. 7、与iOS、Android的交互 高级篇
  7. Android(安卓)安全之 Activity 劫持防护
  8. android移动支付——支付宝支付
  9. android:theme 与 setTheme()设置透明效
  10. android studio 串口通信JNI、NDK配置