Android 谷歌定位服务API,位置不准确
我使用下面的代码来获得用户的准确位置,但是我不得不说,位置是不准确的。它在30米范围内变化,这是不可接受的,如果你想得到关键用途的准确位置。我使用下面的代码(遵循教程)来获得准确的位置,但是它并不精确。任何关于如何获得100%准确位置的提示 Activity.javaAndroid 谷歌定位服务API,位置不准确,android,api,location,Android,Api,Location,我使用下面的代码来获得用户的准确位置,但是我不得不说,位置是不准确的。它在30米范围内变化,这是不可接受的,如果你想得到关键用途的准确位置。我使用下面的代码(遵循教程)来获得准确的位置,但是它并不精确。任何关于如何获得100%准确位置的提示 Activity.java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setConte
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_location);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Nieuwe Locatie Toevoegen");
setSupportActionBar(toolbar);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {}
if(!gps_enabled && !network_enabled) {
// notify user
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("Enable Gps Location plaeast");
dialog.setPositiveButton("Ja ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
//get gps
}
});
dialog.setNegativeButton("Nee liever niet", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
}
});
dialog.show();
}
if (checkGooglePlayServices()) {
buildGoogleApiClient();
//prepare connection request
createLocationRequest();
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
private boolean checkGooglePlayServices() {
int checkGooglePlayServices = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (checkGooglePlayServices != ConnectionResult.SUCCESS) {
/*
* google play services is missing or update is required
* return code could be
* SUCCESS,
* SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED,
* SERVICE_DISABLED, SERVICE_INVALID.
*/
GooglePlayServicesUtil.getErrorDialog(checkGooglePlayServices,
this, REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
return false;
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_RECOVER_PLAY_SERVICES) {
if (resultCode == RESULT_OK) {
// Make sure the app is not already connected or attempting to connect
if (!mGoogleApiClient.isConnecting() &&
!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Google Play Services must be installed.",
Toast.LENGTH_SHORT).show();
finish();
}
}
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
//
// Toast.makeText(this, "Latitude:" + mLastLocation.getLatitude()+", Longitude:"+mLastLocation.getLongitude(),Toast.LENGTH_LONG).show();
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude(), 1);
String city = addresses.get(0).getLocality();
String address = addresses.get(0).getAddressLine(0);
String postalCode = addresses.get(0).getPostalCode();
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("You locatie momenteel is : "+ " " + address + " " + city + " " + postalCode);
dialog.setPositiveButton("Ja dit klopt", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
//Toast.makeText(this, "Stad:" + city + " Straat: "+ address + " Postcode :" + postalCode, Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
// Second Part
@Override
protected void onStart() {
super.onStart();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(20000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
Toast.makeText(this, "Update -> Latitude:" + mLastLocation.getLatitude()+", Longitude:"+mLastLocation.getLongitude(),Toast.LENGTH_LONG).show();
}
protected void stopLocationUpdates() {
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
}
}
@Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
}
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u new\u location);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
toolbar.setTitle(“Nieuwe Locatie Toevoegen”);
设置支持操作栏(工具栏);
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
布尔值gps_enabled=false;
布尔网络_enabled=false;
试一试{
gps_enabled=lm.isProviderEnabled(LocationManager.gps_PROVIDER);
}捕获(例外情况除外){}
试一试{
启用网络\u=lm.isProviderEnabled(LocationManager.network\u提供程序);
}捕获(例外情况除外){}
如果(!gps_已启用&&!网络_已启用){
//通知用户
AlertDialog.Builder dialog=新建AlertDialog.Builder(此);
对话框.setMessage(“启用Gps位置plaeast”);
setPositiveButton(“Ja ok”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface paraloginterface,int paramInt){
//TODO自动生成的方法存根
Intent myIntent=新意图(设置、操作、位置、源、设置);
星触觉(myIntent);
//获取全球定位系统
}
});
setNegativeButton(“Nee liever niet”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface paraloginterface,int paramInt){
//TODO自动生成的方法存根
}
});
dialog.show();
}
如果(选中GooglePlayServices()){
buildGoogleAppClient();
//准备连接请求
createLocationRequest();
}
FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Snackbar.make(查看“替换为您自己的操作”,Snackbar.LENGTH\u LONG)
.setAction(“Action”,null).show();
}
});
}
私有布尔值checkGooglePlayServices(){
int checkGooglePlayServices=GooglePlayServicesUtil
.isGooglePlayServicesAvailable(此);
if(checkGooglePlayServices!=ConnectionResult.SUCCESS){
/*
*缺少google play服务或需要更新
*返回代码可能是
*成功,,
*缺少服务,需要服务版本更新,
*服务\u已禁用,服务\u无效。
*/
GooglePlayServicesUtil.getErrorDialog(选中GooglePlayServices,
这是请求\代码\恢复\播放\服务)。show();
返回false;
}
返回true;
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(requestCode==请求\代码\恢复\播放\服务){
if(resultCode==RESULT\u OK){
//确保应用程序尚未连接或正在尝试连接
如果(!mgoogleapClient.isConnecting())&&
!mGoogleApiClient.isConnected()){
mGoogleApiClient.connect();
}
}else if(resultCode==RESULT\u取消){
Toast.makeText(这是“必须安装Google Play服务”,
吐司。长度(短)。show();
完成();
}
}
}
受保护的同步无效BuildGoogleAppClient(){
mgoogleapclient=新的Googleapclient.Builder(此)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.addApi(LocationServices.API)
.build();
}
@凌驾
未连接的公共空间(捆绑包){
mLastLocation=LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
如果(mLastLocation!=null){
//
//Toast.makeText(这个,“纬度:+mLastLocation.getLatitude()+”,经度:+mLastLocation.getLatitude(),Toast.LENGTH_LONG).show();
地理编码器;
列出地址;
geocoder=新的geocoder(这个,Locale.getDefault());
试一试{
addresses=geocoder.getFromLocation(mLastLocation.getLatitude(),mLastLocation.getLatitude(),1);
字符串city=addresses.get(0.getLocation();
字符串地址=地址.get(0).getAddressLine(0);
字符串postalCode=addresses.get(0.getPostalCode();
AlertDialog.Builder dialog=新建AlertDialog.Builder(此);
setMessage(“您所处的位置消息是:“+”+地址+“+城市+”+邮政编码);
setPositiveButton(“Ja dit klopt”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
}
});
setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
}
});
dialog.show();
//Toast.makeText(这个,“Stad:+city+”Straat:“+address+”邮政编码:“+postalCode,Toast.LENGTH\u LONG).show();
}捕获(IOE异常){
e、 printStackTrace();
}
}
startLocationUpdates();
}
@凌驾
公共空间连接暂停(int i){
}
@凌驾
public void onconnection失败(连接