定位获得当前地址

原创 2015年07月07日 11:33:10

定位大概步奏:

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"
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

php获取ip地址所在的地理位置的实现

1,通过腾讯或者新浪提供的接口来获取(新浪和腾讯类似)    function getIPLocation($queryIP){      $url = 'http://ip.qq.com/cgi-...
  • whq19890827
  • whq19890827
  • 2015年10月01日 22:20
  • 2494

C# 快速定位自己所在位置

最近在项目中遇到了定位的问题,看了看网上的各种定位方法,有通过IP的,还有需要通过注册才能掉用接口的,由于自己不需要定位到很精确,又是定位到本机,所以觉得网上找到的都很麻烦,所以干脆自己找了个新浪的接...
  • XQforCao
  • XQforCao
  • 2017年12月24日 22:17
  • 68

高德自定定位到当前位置

​private MapView mapView; private AMap aMap; private OnLocationChangedListener mListener; private...
  • qq_31043353
  • qq_31043353
  • 2015年11月28日 11:13
  • 3740

iOS定位获取当前所在经纬度,进而获得具体地址(省市地区街道)

首先,我们需要在工程中导入CoreLocation系统框架。然后在我们的控制器中引入头文件。 #import CoreLocation.h> 然后,声明一个CLLocationMana...
  • iOS_gzm
  • iOS_gzm
  • 2016年06月13日 16:14
  • 409

java实现根据ip地址获取地理位置

记录学习使用,转载地址:http://www.jb51.net/article/54287.htm 程序调用了“腾讯ip分享计划”提供的接口,当然也可以改成ip138提供的接口,不过这两个网站返回的字...
  • w410589502
  • w410589502
  • 2016年11月24日 17:43
  • 2233

通过IP地址获取当前地理位置(省份)的接口

1.淘宝接口通过ip获取地址 /** * 通过淘宝IP地址库获取IP位置 *1. 请求接口(GET):http://ip.taobao.com/service/getIpInfo.ph...
  • json_ligege
  • json_ligege
  • 2016年11月29日 18:02
  • 3359

python获得Python脚本所在目录的位置的 【坑】

坑:用os.getcwd()不一定能获得脚本所在的目录一般地,如果是要获得脚本运行的当前目录所在位置,那么可以使用os模块的os.getcwd()函数。但是如果我们想找到脚本所在目录的位置,这种方法就...
  • linzch3
  • linzch3
  • 2017年05月06日 12:20
  • 663

手机端H5获取当前城市的方法

移动端的H5页面提供了定位的功能,那么如何实现一个最简单的需求-----获取用户当前城市? 你可能搜一下就会找到N篇博客介绍,但是你会发现你看完大段代码之后还是没搞清楚,为了便于大家理解,我精简了代...
  • leadn
  • leadn
  • 2017年12月15日 18:47
  • 130

PHP 根据IP地址获取所在城市

有这样的需求,需要根据用户的IP地址,定位用户所在的城市。 本文记录性文章,无逻辑性。有这样需求的朋友,可以直接拷贝使用。直接上代码,不需赘述。 ...
  • liruxing1715
  • liruxing1715
  • 2014年06月03日 10:57
  • 53478

android使用百度地图定位(获取当前经纬度和地址信息)

首先,当然是把需要用到的包导入到工程项目中。(没有包的自己去百度API官网下载) 把 baidumapapi_v3_0_0.jar 和 locSDK_4.2.jar复制粘贴到项目的libs目录下...
  • suqisq
  • suqisq
  • 2015年11月13日 14:30
  • 9435
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:定位获得当前地址
举报原因:
原因补充:

(最多只允许输入30个字)