android - MapboxGeocoding 返回无效的经纬度(Android)
问题描述
我基本上是想从输入的地址中获取纬度和经度,但是 MapboxGeocoding 方法返回的坐标无效,其范围不在 -90 到 90 之间。
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(getString(R.string.access_token))
.query("50 Beale St, San Francisco, CA")
.build();
Log.d(TAG, "THe mapbox geocode: " + mapboxGeocoding.toString());
mapboxGeocoding.enqueueCall(new Callback<GeocodingResponse>() {
@Override
public void onResponse(Call<GeocodingResponse> call, Response<GeocodingResponse> response) {
if (response.body() != null) {
List<CarmenFeature> results = response.body().features();
Log.d(TAG, "carmen response : " + results.toString());
if (results.size() > 0) {
// Log the first results Point.
Point firstResultPoint = results.get(0).center();
Log.d(TAG, "Lat : " + firstResultPoint.longitude());
Log.d(TAG, "long : " + firstResultPoint.latitude());
mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(
new CameraPosition.Builder()
.target(new LatLng(firstResultPoint.longitude(), firstResultPoint.latitude()))
.zoom(17)
.build()), 4000);
Log.d(TAG, "onResponse: " + firstResultPoint.toString());
} else {
// No result for your request were found.
Log.d(TAG, "onResponse: No result found");
}
} else {
Log.d(TAG, "onResponse: Nothing found");
}
}
这部分在这里:
MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
.accessToken(getString(R.string.access_token))
.query("50 Beale St, San Francisco, CA")
.build();
返回 的坐标:
Lat : -122.396457
long : 37.791239
这是完全错误的,我的日志:
12-04 10:52:21.531 11351-11351/io.apptizer.business.clover E/AndroidRuntime: FATAL EXCEPTION: main
Process: io.apptizer.business.clover, PID: 11351
java.lang.IllegalArgumentException: latitude must be between -90 and 90
at com.mapbox.mapboxsdk.geometry.LatLng.setLatitude(LatLng.java:132)
at com.mapbox.mapboxsdk.geometry.LatLng.<init>(LatLng.java:67)
at io.apptizer.pos.activity.SearchLocationActivity$2.onResponse(SearchLocationActivity.java:222)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
为什么会发生这种情况,是因为我使用的是公共 mapbox 密钥吗?
解决方案
我相信
.target(new LatLng(firstResultPoint.longitude(), firstResultPoint.latitude()))
应该
.target(new LatLng(firstResultPoint.latitude(), firstResultPoint.longitude()))
正如 LatLng 的名字所暗示的那样。
推荐阅读
- sql - 尝试使用 OR 语句实现 2 个外连接。语法不正确?
- django - 如何在 Django DRF 的 GenericAPIView 中正确使用查询集?
- c - leetcode 38 的一个 c 解决方案 count and say 迷惑我
- c - 为什么指针数组的 free() 不会释放内存?
- java - log4j2 JSON 为根记录器配置多个附加程序
- regex - Fluentd 和 Docker
- ruby-on-rails - MyMethod 存在时 NoMethodError
- html - 如何在列图像上添加文本
- html - 提交后如何刷新部分页面?
- typescript - 使用 Angular 5 firebase 创建具有电子邮件和密码的用户