我在模拟器上测试是可以的,但在真机上好像监听不到,监听器里面的程序没有执行。
//创建LocationManager对象
locationManager = (LocationManager)ShopsActivity.this.getSystemService(Context.LOCATION_SERVICE);
//绑定监听器,第二个参数表示更新的最小时间(毫秒);第三个参数表示更新的最小距离(米)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, new MyLocationListener());private class MyLocationListener implements LocationListener
{
//位置变化时调用
@Override
public void onLocationChanged(Location location) {
ProBarLocationShops.setVisibility(View.GONE);
String latlng= location.getLongitude()+","+location.getLatitude();
TvLocationShops.setText(latlng); //这个都执行不到,
String url = "http://maps.google.com/maps/api/geocode/json?sensor=false&latlng="+latlng;
//url = "http://maps.google.com/maps/api/geocode/json?sensor=false&latlng=40.124356,-73.961472";
try
{
String jsonData = new HttpHelper().getHttpContent(url);
Gson gson = new Gson();
GoogleMapData mapData = gson.fromJson(jsonData,GoogleMapData.class);
TvLocationShops.setText(mapData.getResults().get(0).getFormatted_address());
}
catch(Exception ex)
{
System.out.println("Google Map Excetpion--->"+ex.toString());
TvLocationShops.setText("暂时无法定位到您的位置");
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
} @Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
} @Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}为什么会这样,
求大神帮助了
//创建LocationManager对象
locationManager = (LocationManager)ShopsActivity.this.getSystemService(Context.LOCATION_SERVICE);
//绑定监听器,第二个参数表示更新的最小时间(毫秒);第三个参数表示更新的最小距离(米)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, new MyLocationListener());private class MyLocationListener implements LocationListener
{
//位置变化时调用
@Override
public void onLocationChanged(Location location) {
ProBarLocationShops.setVisibility(View.GONE);
String latlng= location.getLongitude()+","+location.getLatitude();
TvLocationShops.setText(latlng); //这个都执行不到,
String url = "http://maps.google.com/maps/api/geocode/json?sensor=false&latlng="+latlng;
//url = "http://maps.google.com/maps/api/geocode/json?sensor=false&latlng=40.124356,-73.961472";
try
{
String jsonData = new HttpHelper().getHttpContent(url);
Gson gson = new Gson();
GoogleMapData mapData = gson.fromJson(jsonData,GoogleMapData.class);
TvLocationShops.setText(mapData.getResults().get(0).getFormatted_address());
}
catch(Exception ex)
{
System.out.println("Google Map Excetpion--->"+ex.toString());
TvLocationShops.setText("暂时无法定位到您的位置");
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
} @Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
} @Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}为什么会这样,
求大神帮助了
private static GpsListener call;
private List<GpsSatellite> numSatelliteList = new ArrayList<GpsSatellite>(); // 卫星信号
private static GpsManager mGpsListener;
public void RegisteListener(Context _context, GpsListener _call){
context = _context; call = _call;
getLocation();
} LocationManager locationManager; private void getLocation() {
// 获取位置管理服务
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) context
.getSystemService(serviceName);
// 获取GPS信息
String provider = LocationManager.GPS_PROVIDER; Location location = locationManager.getLastKnownLocation(provider);// 通过GPS获取位置 if (location == null)
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // updateToNewLocation(location);
// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000)或最小位移变化超过N米
locationManager.requestLocationUpdates(provider, 1000, 0, this);
locationManager.addGpsStatusListener(this); // 注册状态信息回调
} private void updateToNewLocation(Location location) {
// 获取系统时间
Time t = new Time();
t.setToNow(); // 取得系统时间
int year = t.year;
int month = t.month + 1;
int date = t.monthDay;
int hour = t.hour; // 24小时制
int minute = t.minute;
int second = t.second; if (location != null) {
double latitude = location.getLatitude();// 经度
double longitude = location.getLongitude();// 纬度
double altitude = location.getAltitude(); // 海拔 call.SetSatellite("搜索卫星个数:" + numSatelliteList.size());
call.SetLocation(new GPSPoint(longitude, latitude, 0, 0, 0, 0, GPSPoint.TYPE_CART));
call.SetLatestUpdateTime("/n时间:" + year + "年" + month + "月" + date + "日"
+ hour + ":" + minute + ":" + second);
} else { call.SetSatellite("无法获取地理信息");
}
} public void onLocationChanged(Location location) {
// 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
if (location != null) {
updateToNewLocation(location); }
} public void onProviderDisabled(String provider) {
// Provider被disable时触发此函数,比如GPS被关闭
updateToNewLocation(null);
} public void onProviderEnabled(String provider) {
// Provider被enable时触发此函数,比如GPS被打开
} public void onStatusChanged(String provider, int status, Bundle extras) {
// Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
} @Override
public void onGpsStatusChanged(int event) {
GpsStatus status = locationManager.getGpsStatus(null); // 取当前状态
updateGpsStatus(event, status);
} private void updateGpsStatus(int event, GpsStatus status) {
if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
int maxSatellites = status.getMaxSatellites();
Iterator<GpsSatellite> it = status.getSatellites().iterator();
numSatelliteList.clear();
int count = 0;
while (it.hasNext() && count <= maxSatellites) {
GpsSatellite s = it.next();
numSatelliteList.add(s);
count++;
}
}
} public interface GpsListener { // public void RegisteCallback();
public void SetLocation(GPSPoint point); public void SetSatellite(String numberInfo);
public void SetLatestUpdateTime(String time); }
}