定位获得当前地址

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

相关文章推荐

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

前言: 今天做iOS项目的时候,需要通过定位来拿到当期城市的名称。百度地图SDK有这个功能,但为了不依赖第三方,这里我用iOS自带框架CoreLocation来实现这个需求。iOS8出来之后,针对定位...

AngularJs 利用百度地图API 定位当前位置 获取地址信息

利用angular+百度地图API实现定位获取当前地址信息

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

首先,把你申请的key配置到appliation。

百度地图BaiduMap--学习(四)------定位当前位置,显示中文地址

转载学习http://blog.csdn.net/bob_xing_yang/article/details/50450544 直接上代码: xml
  • xiey94
  • xiey94
  • 2016年10月16日 17:16
  • 484

获得当前网页的地址

  • 2007年11月26日 11:23
  • 5KB
  • 下载

Android-快速获得当前所在地理位置(简单定位)

1.思路分析 首先获得当前自己所在位置的经纬度。把我们获得的经纬度转换成我们能看得懂的具体位置信息 2.具体逻辑 借助Android本身的LocationManager这个类提供的方法来...
  • jc_0203
  • jc_0203
  • 2016年04月13日 15:20
  • 16691

获得Android系统的唯一标识、android版本、网络接入方式、当前网络接入方式的MAC地址、IP、CPU的利用率、CPU的负载、内存大小

package com.net.app; import java.io.BufferedReader; import java.io.File; import java.io.FileRea...

通过新浪IP服务器获得的当前客户端IP地址对应的国家、省份或直辖市、城市信息

通过新浪IP服务器获得的当前客户端IP地址对应的国家、省份或直辖市、城市信息...

JS获得当前地址栏url

URL即:统一资源定位符 (Uniform Resource Locator, URL) 完整的URL由这几个部分构成: scheme://host:port/path?query#fragmen...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:定位获得当前地址
举报原因:
原因补充:

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