关闭

定位获得当前地址

195人阅读 评论(0) 收藏 举报
分类:

定位大概步奏:

1.通过gps获Ip地址获得当前移动设备的经纬度。

2.通过http访问google地图网址返回的json文件获得解析出来的地址。

 

主要代码:

public void registerLocationListener() {

LocationManager locationManager = (LocationManager) context
    .getSystemService(Context.LOCATION_SERVICE);
  networkListner = new MyLocationListener();
  locationManager.requestLocationUpdates(
    LocationManager.NETWORK_PROVIDER, 3000, 0, networkListner);
  gpsListener = new MyLocationListener();
  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
    5000, 0, gpsListener);

 }

 private class MyLocationListener implements LocationListener {

  public void onLocationChanged(Location location) {
   Log.i("locationManager", locationManager.getAllProviders()
     .toString());
   if (currentLocation != null) {
    if (isBetterLocation(location, currentLocation)) {
     Log.v("GPSTEST", "It's a better location");
     currentLocation = location;
     showLocation(location);
    } else {
     Log.v("GPSTEST", "Not very good!");
    }
   } else {
    Log.v("GPSTEST", "It's first location");
    currentLocation = location;
    showLocation(location);
   }
   // 移除基于LocationManager.NETWORK_PROVIDER的监听器
   if (LocationManager.NETWORK_PROVIDER.equals(location.getProvider())) {
    locationManager.removeUpdates(this);
   }

  }

  public void onStatusChanged(String provider, int status, Bundle extras) {

  }

  public void onProviderEnabled(String provider) {

  }

  public void onProviderDisabled(String provider) {

  }

 }

 private void showLocation(Location location) {
  mLatitude = location.getLatitude();
  mLongitude = location.getLongitude();
  Log.i("mLatitude", mLatitude + "");
  Log.i("mLongitude", mLongitude + "");
  String url = String.format(http, mLatitude, mLongitude);
  TestResult result = DownloadUtil.downloadByUrl(url,
    Charset.defaultCharset());
  activity.getAddress(jsonSax(result));

 }

 private static String[] jsonSax(TestResult result) {
  String[] address = new String[3];
  if (result != null) {
   if (!result.getStatus().equals("OK")) {
    address[0] = "无法获取具体地址";
   } else {
    address[0] = result.getResults().get(1).getFormatted_address();
    address[1] = result.getResults().get(2).getAddress_components()
      .get(1).getLong_name();
    address[2] = result.getResults().get(2).getAddress_components()
      .get(0).getLong_name();
   }
  } else {
   address[0] = "无法获取具体地址";
  }
  Log.i("address", address[1]);
  return address;
 }

 private static final int CHECK_INTERVAL = 1000 * 30;

 protected boolean isBetterLocation(Location location,
   Location currentBestLocation) {
  if (currentBestLocation == null) {
   return true;
  }

  long timeDelta = location.getTime() - currentBestLocation.getTime();
  boolean isSignificantlyNewer = timeDelta > CHECK_INTERVAL;
  boolean isSignificantlyOlder = timeDelta < -CHECK_INTERVAL;
  boolean isNewer = timeDelta > 0;

  if (isSignificantlyNewer) {
   return true;
  } else if (isSignificantlyOlder) {
   return false;
  }

  int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation
    .getAccuracy());
  boolean isLessAccurate = accuracyDelta > 0;
  boolean isMoreAccurate = accuracyDelta < 0;
  boolean isSignificantlyLessAccurate = accuracyDelta > 200;

  boolean isFromSameProvider = isSameProvider(location.getProvider(),
    currentBestLocation.getProvider());

  if (isMoreAccurate) {
   return true;
  } else if (isNewer && !isLessAccurate) {
   return true;
  } else if (isNewer && !isSignificantlyLessAccurate
    && isFromSameProvider) {
   return true;
  }
  return false;
 }

 private boolean isSameProvider(String provider1, String provider2) {
  if (provider1 == null) {
   return provider2 == null;
  }
  return provider1.equals(provider2);
 }
json文件:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "珠江路",
               "short_name" : "S320",
               "types" : [ "route" ]
            },
            {
               "long_name" : "北仑区",
               "short_name" : "北仑区",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "宁波",
               "short_name" : "宁波",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "浙江省",
               "short_name" : "浙江省",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "中国",
               "short_name" : "CN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "中国浙江省宁波市北仑区珠江路",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 29.90676640,
                  "lng" : 121.86781210
               },
               "southwest" : {
                  "lat" : 29.90103360,
                  "lng" : 121.86667790
               }
            },
            "location" : {
               "lat" : 29.90390020,
               "lng" : 121.86724390
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 29.90676640,
                  "lng" : 121.8685939802915
               },
               "southwest" : {
                  "lat" : 29.90103360,
                  "lng" : 121.8658960197085
               }
            }
         },
         "types" : [ "route" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "北仑区",
               "short_name" : "北仑区",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "宁波",
               "short_name" : "宁波",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "浙江省",
               "short_name" : "浙江省",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "中国",
               "short_name" : "CN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "中国浙江省宁波市北仑区",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 29.99079930,
                  "lng" : 122.15796280
               },
               "southwest" : {
                  "lat" : 29.70892750,
                  "lng" : 121.65269810
               }
            },
            "location" : {
               "lat" : 29.8988660,
               "lng" : 121.8446690
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 29.99079930,
                  "lng" : 122.15796280
               },
               "southwest" : {
                  "lat" : 29.70892750,
                  "lng" : 121.65269810
               }
            }
         },
         "types" : [ "sublocality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "宁波",
               "short_name" : "宁波",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "浙江省",
               "short_name" : "浙江省",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "中国",
               "short_name" : "CN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "中国浙江省宁波市",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 30.36904030,
                  "lng" : 122.27992880
               },
               "southwest" : {
                  "lat" : 28.85352960,
                  "lng" : 120.91691940
               }
            },
            "location" : {
               "lat" : 29.8683360,
               "lng" : 121.543990
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 30.05437520,
                  "lng" : 121.80541990
               },
               "southwest" : {
                  "lat" : 29.68518539999999,
                  "lng" : 121.3385010
               }
            }
         },
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "浙江省",
               "short_name" : "浙江省",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "中国",
               "short_name" : "CN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "中国浙江省",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 31.17878220,
                  "lng" : 122.95566510
               },
               "southwest" : {
                  "lat" : 27.04668010,
                  "lng" : 118.0282790
               }
            },
            "location" : {
               "lat" : 30.265860,
               "lng" : 120.1536760
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 31.17878220,
                  "lng" : 122.95566510
               },
               "southwest" : {
                  "lat" : 27.04668010,
                  "lng" : 118.0282790
               }
            }
         },
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "中国",
               "short_name" : "CN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "中国",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 53.56097399999999,
                  "lng" : 134.77280990
               },
               "southwest" : {
                  "lat" : 18.15352160,
                  "lng" : 73.49941360
               }
            },
            "location" : {
               "lat" : 35.861660,
               "lng" : 104.1953970
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 53.56097399999999,
                  "lng" : 134.77280990
               },
               "southwest" : {
                  "lat" : 18.15352160,
                  "lng" : 73.49941360
               }
            }
         },
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:24108次
    • 积分:202
    • 等级:
    • 排名:千里之外
    • 原创:95篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档