前段时间在弄GPRS定位的问题,使用google的地图定位,大家也都知道,google现在在中国境内有很多限制,而且国外刷机严重,难免将google的各种服务给刷掉,所以最终采用百度的定位系统,完美实现。现在有时间了,给大家讲一讲,代码并不多。
我还是先说说google的定位吧,说不定有些仁兄需要的呢!
首先判断机器的GPRS模块是否正常,如果不正常,那没办法了,哪家的定位系统都不能用。
[html]
LocationManager alm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
return;
}
LocationManager alm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
return;
}设置开启GPRS页面
[html]
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面
设置省电模式,获得最好的定位方式
[html]
ocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
gprs_view = (TextView) findViewById(R.id.gprs_view);
Criteria criteria = new Criteria();
// 获得最好的定位效果
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
// 使用省电模式
criteria.setPowerRequirement(Criteria.POWER_LOW);
// 获得当前的位置提供者
provider = locationManager.getBestProvider(criteria, false);
ser.append(provider);
locationManager.requestLocationUpdates(provider, 2000, 10, this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
gprs_view = (TextView) findViewById(R.id.gprs_view);
Criteria criteria = new Criteria();
// 获得最好的定位效果
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
// 使用省电模式
criteria.setPowerRequirement(Criteria.POWER_LOW);
// 获得当前的位置提供者
provider = locationManager.getBestProvider(criteria, false);
ser.append(provider);
locationManager.requestLocationUpdates(provider, 2000, 10, this);
获得上次location对象
[html]
// 使用网络定位,获得上次定位的location对象
if (location == null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
provider = LocationManager.NETWORK_PROVIDER;
}
// 使用网络定位,获得上次定位的location对象
if (location == null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
provider = LocationManager.NETWORK_PROVIDER;
}
然后定位
[html]
String latLongString;
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "纬度:" + lat + "\n经度:" + lng;
Geocoder gc = new Geocoder(context);
List<Address> addresses = null;
try {
addresses = gc.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ser.append("\n" + addresses.get(0).getCountryName());
} else {
latLongString = "无法获取地理信息";
}
ser.append("\n" + "您当前的位置是:\n" + latLongString);
String latLongString;
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "纬度:" + lat + "\n经度:" + lng;
Geocoder gc = new Geocoder(context);
List<Address> addresses = null;
try {
addresses = gc.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ser.append("\n" + addresses.get(0).getCountryName());
} else {
latLongString = "无法获取地理信息";
}
ser.append("\n" + "您当前的位置是:\n" + latLongString);
实现LocationListener接口,并在onLocationChanged和onProviderDisabled方法中实现updateWithNewLocation方法
以期待在未获得location对象时,不断获取直到取到为止
[html]
private void updateWithNewLocation(Location location) {
// TODO Auto-generated method stub
if (location == null) {
locationManager.requestLocationUpdates(provider, 2000, (float) 0.1,
this);
}
}
private void updateWithNewLocation(Location location) {
// TODO Auto-generated method stub
if (location == null) {
locationManager.requestLocationUpdates(provider, 2000, (float) 0.1,
this);www.zzzyk.com
}
}
以上是我弄到的关于用google开发服务的资料,实际上次定位的位置很难得到,实现定位,比较困难,也许是笔者使用的是水货,刷过机的原因吧。