我最近在研究网络定位,遇到一些问题,能得到provider为network,但是得到的location却为0,不知道什么原因,GPS定位可以成功,但是网络定位就是不行。下面上代码,求高人指点 private void location2(){
//获取到LocationManager对象
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
provider=LocationManager.GPS_PROVIDER;
locationManager.requestLocationUpdates(provider, 10000, 0, locationListener2);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
lat = location.getLatitude();
lon = location.getLongitude();
System.out.println("------>"+"GPS定位成功");
}
else
{
provider=LocationManager.NETWORK_PROVIDER;
if(provider!=null){
locationManager.requestLocationUpdates(provider, 10000, 0, locationListener2);
}else{
lat = 0;
lon = 0;
System.out.println("------>"+"请开启GPS和网络定位");
}
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null)
{
lat = location.getLatitude();
lon = location.getLongitude();
System.out.println("------>"+"GPS已开启但无法定位,network定位成功");
// strAddress = getAddress(latitude, longitude);
}
else
{
lat = 0;
lon = 0;
System.out.println("------>"+"GPS和network已开启但无法定位");
}
}
}else if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
provider=LocationManager.NETWORK_PROVIDER;
locationManager.requestLocationUpdates(provider, 10000, 0, locationListener2);
System.out.println("------>是"+provider);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null)
{
lat = location.getLatitude();
lon = location.getLongitude();
System.out.println("------>"+"GPS未开启,network定位成功");
// strAddress = getAddress(latitude, longitude);
}
else
{
lat = 0;
lon = 0;
System.out.println("------>"+"GPS未开启,network已开启但无法定位");
}
}else{
lat = 0;
lon = 0;
System.out.println("------>"+"GPS和network均未开启");
}
}
//创建位置监听器
private LocationListener locationListener2 = new LocationListener(){
//位置发生改变时调用
@Override
public void onLocationChanged(Location location) {
Log.d("Location", "onLocationChanged");
lat=location.getLatitude();
lon=location.getLongitude();
}
//provider失效时调用
@Override
public void onProviderDisabled(String provider) {
Log.d("Location", "onProviderDisabled");
}
//provider启用时调用
@Override
public void onProviderEnabled(String provider) {
Log.d("Location", "onProviderEnabled");
}
//状态改变时调用
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Location", "onStatusChanged");
}
};
//获取到LocationManager对象
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
provider=LocationManager.GPS_PROVIDER;
locationManager.requestLocationUpdates(provider, 10000, 0, locationListener2);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
lat = location.getLatitude();
lon = location.getLongitude();
System.out.println("------>"+"GPS定位成功");
}
else
{
provider=LocationManager.NETWORK_PROVIDER;
if(provider!=null){
locationManager.requestLocationUpdates(provider, 10000, 0, locationListener2);
}else{
lat = 0;
lon = 0;
System.out.println("------>"+"请开启GPS和网络定位");
}
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null)
{
lat = location.getLatitude();
lon = location.getLongitude();
System.out.println("------>"+"GPS已开启但无法定位,network定位成功");
// strAddress = getAddress(latitude, longitude);
}
else
{
lat = 0;
lon = 0;
System.out.println("------>"+"GPS和network已开启但无法定位");
}
}
}else if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
provider=LocationManager.NETWORK_PROVIDER;
locationManager.requestLocationUpdates(provider, 10000, 0, locationListener2);
System.out.println("------>是"+provider);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null)
{
lat = location.getLatitude();
lon = location.getLongitude();
System.out.println("------>"+"GPS未开启,network定位成功");
// strAddress = getAddress(latitude, longitude);
}
else
{
lat = 0;
lon = 0;
System.out.println("------>"+"GPS未开启,network已开启但无法定位");
}
}else{
lat = 0;
lon = 0;
System.out.println("------>"+"GPS和network均未开启");
}
}
//创建位置监听器
private LocationListener locationListener2 = new LocationListener(){
//位置发生改变时调用
@Override
public void onLocationChanged(Location location) {
Log.d("Location", "onLocationChanged");
lat=location.getLatitude();
lon=location.getLongitude();
}
//provider失效时调用
@Override
public void onProviderDisabled(String provider) {
Log.d("Location", "onProviderDisabled");
}
//provider启用时调用
@Override
public void onProviderEnabled(String provider) {
Log.d("Location", "onProviderEnabled");
}
//状态改变时调用
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Location", "onStatusChanged");
}
};
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货