package com.studio.android.chp08.ex01;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class CurrentLocation extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(serviceName);
// String provider = LocationManager.GPS_PROVIDER;
String provider = LocationManager.NETWORK_PROVIDER;
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
// String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "纬度:" + lat + "\n经度:" + lng;
} else {
latLongString = "无法获取地理信息";
}
List<Address> addList = null;
Geocoder ge = new Geocoder(this);
try {
addList = ge.getFromLocation(24.463, 118.1, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(addList!=null && addList.size()>0){
for(int i=0; i<addList.size(); i++){
Address ad = addList.get(i);
latLongString += "\n";
latLongString += ad.getCountryName() + ";" + ad.getLocality();
}
}
myLocationText.setText("您当前的位置是:\n" +latLongString);
}
}
分享到:
相关推荐
Android 根据经纬度获取地址信息 包括:名称 地址 经纬度 电话 邮编等
android根据经纬度获取地址,很准的
获取经纬度已经封装成工具类,纯原生获取android手机经纬度,并且根据经纬度转换成准确的地址
本项目基于spring boot实现根据经纬度坐标返回所在行政区域。
通过Androidstudio3.0对于百度地图进行开发,主要实现通过经纬度修改定位,改变地图显示类型(普通、卫星、热力、实况等)
java 根据经纬度获取地理位置
全国城市经纬度定位json资源。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
Android使用Google Map API创建的一个根据经纬度定位的程序. 交流QQ群:1279871
获取经纬度已经封装成工具类,纯原生获取android手机经纬度,并且根据经纬度转换成准确的地址
根据经纬度CLLcoordinate2D获取城市区号,赞不包含新疆,西藏,如有问题,请留言,相互学习。
根据IP或经纬度查询城市-离线版 根据IP或经纬度查询城市-离线版
xamarin android中使用gps定位获取经纬度,入门的简单介绍:http://blog.csdn.net/kebi007/article/details/74936979
在Android应用中实现GPS返回Location经纬度值 不为null 可以实现
代码文件要求在android2.1以上版本可用,可以实现android定位,显示出定位的经纬度,并且可显示出该经纬度所代表的街道地址,本代码主要是应用baidu进行定位,解决了google在大陆地区无牌照没法定位的问题。...
经纬度定位
android通过network获取当前的经纬度和地理位置
该项目可以在Android studio上直接运行,并显示当前具体地理位置(经纬度,城市,街道等)
NULL 博文链接:https://yafei.iteye.com/blog/1396200
解决网络上常见的location返回为null问题,室内真机测试可能达不到结果,但是室外一定可以
根据经纬度实时定位,iOS8最新功能,可以根据经纬度准确定位自己的地理位置。