gpt4 book ai didi

Android Studio 'Device Only' 位置 w/Google Play 服务不工作

转载 作者:行者123 更新时间:2023-11-30 01:54:54 25 4
gpt4 key购买 nike

我正在开发我的第一个应用程序,它是一个非常简单的应用程序。单击按钮将经度/纬度发送到服务器/数据库。我正在使用 Google Play 服务进行定位,并使用 Nexus 9 平板电脑进行测试。我发现使用 Settings>Location (on Nexus 9) - High Accuracy & Low Accuracy 效果很好。数据正确并发送到服务器。但是当选择“仅设备”(GPS)时,它找不到经/纬度。我 toast :

Toast.makeText(MainActivity.this, "Network isn't available", Toast.LENGTH_LONG).show();

我对这个还是陌生的,所以我不确定我可能哪里出错了。我确实有 list 文件 w/

uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" 

还有我的 Gradle 文件中的 Google Play 服务。因为无论如何它都可以使用 WiFi。

我也放了

System.out.println("Longitude (function name)" + longitude + " and Latitude: " + latitude);

当 Long/Lat 被拾取时在日志中查看(仅当单击按钮时,否则为 0.0)在我上次测试中,我使用了高精度并获得了 Long/Lat。关闭 Wifi 并更改位置,它正在获取我的新位置(但无法发送数据,因为没有互联网连接)。所以我很困惑,因为 GPS 不是以高精度获取我的新位置吗?

不确定这是否重要,但我希望“仅设备”适用于定位和高精度。任何帮助将不胜感激!这是我的代码(已缩短,如果我遗漏了任何内容,请让我知道要添加!)谢谢!

public class MainActivity extends Activity implements ConnectionCallbacks,
OnConnectionFailedListener {

private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private Button btnNewShowLocation;
private double longitude;
private double latitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (checkPlayServices()) {
buildGoogleApiClient();
//**UPDATED**
createLocationRequest()
}
btnNewShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mLastLocation != null) {
postDataToServer("web site address- EDIT");
} else {
Toast.makeText(MainActivity.this, "Network isn't available", Toast.LENGTH_LONG).show();
}}});}
// **UPDATED**
protected void createLocationRequest() {
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setInterval(UPDATE_INTERVAL)
.setFastestInterval(FATEST_INTERVAL)
.setSmallestDisplacement(DISPLACEMENT);

private void postDataToServer(String uri) {

RequestPackage p = new RequestPackage();
p.setMethod("POST");
p.setUri(uri);
p.setParam("longitude", String.valueOf(longitude));
p.setParam("latitude", String.valueOf(latitude));
MyTask task = new MyTask();
task.execute(p);
System.out.println("Longitude (post data to server)" + longitude + " and Latitude: " + latitude);

}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
}
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Toast.makeText(getApplicationContext(),
"This device is not supported.", Toast.LENGTH_LONG)
.show();
finish();
}
return false;
}
return true;
}
@Override
public void onConnected(Bundle arg0) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
System.out.println("Longitude (on Connected): " + longitude + " and Latitude: " + latitude);

if (mLastLocation != null) {
latitude = mLastLocation.getLatitude();
longitude = mLastLocation.getLongitude();
} else {
Toast.makeText(getApplicationContext(), "Searching for your location... \nMake sure WiFi or GPS is turned On", Toast.LENGTH_LONG).show();
}}}

这里是高精度开启和 Wifi 关闭的日志注释。

I/System.out﹕ Longitude (on Connected): 0.0 and Latitude: 0.0
I/System.out﹕ Longitude (post data to server)-88.7777777 and Latitude: 55.7777777
W/System.err﹕ java.net.UnknownHostException: Unable to resolve host

最后一行的 Wifi 已关闭。

最佳答案

您需要创建LocationRequest 对象并执行以下操作:

// Create the LocationRequest object
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setInterval(interval)
.setFastestInterval(fastestInterval)
.setSmallestDisplacement(minDisplacement);

更多详情,可以试试我的代码here .

关于Android Studio 'Device Only' 位置 w/Google Play 服务不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32399425/

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