【发布时间】:2015-11-30 16:37:55
【问题描述】:
我正在开发我的第一个应用程序,它是一个非常简单的应用程序。单击按钮将经度/纬度发送到服务器/数据库。我正在使用 Google Play 服务进行定位,并且正在使用 Nexus 9 平板电脑进行测试。 我发现使用 Settings>Location (on Nexus 9) - High Accuracy & Low Accuracy 工作正常。数据正确并发送到服务器。但是在选择“仅设备”(GPS)时,它找不到LONG / LAT。我得到了我的吐司:
Toast.makeText(MainActivity.this, "Network isn't available", Toast.LENGTH_LONG).show();
我还是新手,所以我不确定我可能哪里出错了。我确实有清单文件 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 不是在 High Accuracy 中获取我的新位置吗?
不确定这是否重要,但我希望仅设备用于定位以及高精度。 任何帮助将不胜感激!这是我的代码(缩短,如果我遗漏任何内容,请告诉我添加!)谢谢!
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();
}}}
这是 High Accuracy on 和 Wifi off 的日志记录。
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对象和setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY) .setInterval(interval) .setFastestInterval(fastestInterval) .setSmallestDisplacement(minDisplacement);更多细节,你可以试试我的代码here。 -
感谢@bjiang 我确实尝试过,它确实工作得短暂。现在不是。我把它放在一个函数'protected void createLocationRequest() { mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY) .setInterval(UPDATE_INTERVAL) .setFastestInterval(FATEST_INTERVAL) .setSmallestDisplacement(DISPLACEMENT);'并在 OnCreate 中调用它。在调试器中,我得到“仅设备”的 getLastLocation = null 。我会继续努力的
-
你试过我的代码here吗?
-
@bjiang,是的,我确实尝试了您的代码。很不错!谢谢! LocationRequest 对象毕竟成功了。问题已解决
-
很高兴为您提供帮助,干杯!这个我贴了一个答案,如果你觉得我帮到了你,可以采纳哦~~
标签: android google-play-services