@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
LocationManager tmpLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (tmpLocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
{
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent =new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent,0); //此为设置完成后返回到获取界面
Criteria c=new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
c.setAltitudeRequired(false);
c.setBearingRequired(false);
c.setCostAllowed(true);
c.setPowerRequirement(Criteria.POWER_LOW);
String provider = tmpLocationManager.getBestProvider(c, true);
Location location = tmpLocationManager.getLastKnownLocation(provider);
//preTime=System.currentTimeMillis();
updateWithNewLocation(location);
tmpLocationManager.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.MaptextView);
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "纬度:" + lat + "\n经度:" + lng;
} else {
latLongString = "无法获取地理信息";
}
myLocationText.setText("您当前的位置是:\n" +
latLongString);
}
以上代码有何错误?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货