编写了一个android下的wifi定位程序,连好wifi后发布到真机上运行,结果无法得到经纬度。求大神帮忙看看。 我在真机的设置选项里,已经选上了网络定位选项。代码如下:想实现在log中打印经纬度,并在UI上显示地理信息。 我在运行时,打印的经纬度均为0;也得不到地理信息。
package com.example.wifitest;
import java.io.IOException;
import java.util.List;import android.app.Activity;
import android.os.Bundle;
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.util.Log;
import android.widget.Toast;public class MainActivity extends Activity {
//创建位置监听器
private LocationListener locationListener = new LocationListener(){
//位置发生改变时调用
@Override
public void onLocationChanged(Location location) {
Log.d("Location", "onLocationChanged Latitude" + location.getLatitude());
Log.d("Location", "onLocationChanged Longitude" + 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");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取到LocationManager对象
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
String currentProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER).getName();
Location currentLocation = locationManager.getLastKnownLocation(currentProvider);
//如果位置信息为null,则请求更新位置信息
if(currentLocation == null){
locationManager.requestLocationUpdates(currentProvider, 1, 0, locationListener);
}
while(true){
currentLocation = locationManager.getLastKnownLocation(currentProvider);
if(currentLocation != null){
Log.d("Location", "Latitude: " + currentLocation.getLatitude());
Log.d("Location", "location: " + currentLocation.getLongitude());
break;
}else{
Log.d("Location", "Latitude: " + 0);
Log.d("Location", "location: " + 0);
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Log.e("Location", e.getMessage());
}
}
//解析地址并显示
Geocoder geoCoder = new Geocoder(this);
try {
int latitude = (int) currentLocation.getLatitude();
int longitude = (int) currentLocation.getLongitude();
List<Address> list = geoCoder.getFromLocation(latitude, longitude, 2);
for(int i=0; i<list.size(); i++){
Address address = list.get(i);
Toast.makeText(MainActivity.this, address.getCountryName() + address.getAdminArea() + address.getFeatureName(), Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
package com.example.wifitest;
import java.io.IOException;
import java.util.List;import android.app.Activity;
import android.os.Bundle;
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.util.Log;
import android.widget.Toast;public class MainActivity extends Activity {
//创建位置监听器
private LocationListener locationListener = new LocationListener(){
//位置发生改变时调用
@Override
public void onLocationChanged(Location location) {
Log.d("Location", "onLocationChanged Latitude" + location.getLatitude());
Log.d("Location", "onLocationChanged Longitude" + 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");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取到LocationManager对象
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
String currentProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER).getName();
Location currentLocation = locationManager.getLastKnownLocation(currentProvider);
//如果位置信息为null,则请求更新位置信息
if(currentLocation == null){
locationManager.requestLocationUpdates(currentProvider, 1, 0, locationListener);
}
while(true){
currentLocation = locationManager.getLastKnownLocation(currentProvider);
if(currentLocation != null){
Log.d("Location", "Latitude: " + currentLocation.getLatitude());
Log.d("Location", "location: " + currentLocation.getLongitude());
break;
}else{
Log.d("Location", "Latitude: " + 0);
Log.d("Location", "location: " + 0);
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Log.e("Location", e.getMessage());
}
}
//解析地址并显示
Geocoder geoCoder = new Geocoder(this);
try {
int latitude = (int) currentLocation.getLatitude();
int longitude = (int) currentLocation.getLongitude();
List<Address> list = geoCoder.getFromLocation(latitude, longitude, 2);
for(int i=0; i<list.size(); i++){
Address address = list.get(i);
Toast.makeText(MainActivity.this, address.getCountryName() + address.getAdminArea() + address.getFeatureName(), Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货