gpt4 book ai didi

java - 无需谷歌地图即可获取实际位置的纬度和日志

转载 作者:行者123 更新时间:2023-12-01 08:59:16 27 4
gpt4 key购买 nike

我尝试了基于网络和 GPS 提供商的代码来获取设备所在的完美位置,但经纬度始终不准确。我用过http://www.vogella.com/tutorials/AndroidLocationAPI/article.html获取值并得到 lat 为 19.10295898 和 lon 为 72.8866532。通过谷歌地图,纬度、经度分别为 19.103114,72.8867978。有差别。我们实际上可以以某种方式匹配它吗?使用的代码如下:

package jss.fusedlocation;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.nearby.messages.Distance;

/**
* Created by DELL WORLD on 1/23/2017.
*/

public class ShowLocationActivity extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latituteField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);

// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
location.setAccuracy(1);

// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
}

/* Request updates at startup */
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 1, 1, this);
}

/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
double lat = (double) (location.getLatitude());
double lng = (double) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}

最佳答案

I tried network and GPs provider based codes to get perfect location where the device is

一般来说,无论如何都不可能“获得设备所在的完美位置”。获取位置涉及许多变数,但并非所有这些变数始终对您有利。

Can we actually match it somehow

据我们所知,Google map 使用 Play Services SDK 的 fused location provider .

关于java - 无需谷歌地图即可获取实际位置的纬度和日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41810030/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com