Android:反向地理编码 - getFromLocation
我正在尝试根据经/纬度获取地址。 看来这样的事情应该有效?
Geocoder myLocation = Geocoder(Locale.getDefault());
List myList = myLocation.getFromLocation(latPoint,lngPoint,1);
问题是我不断收到: The method Geocoder(Locale) is undefined for the type savemaplocation
任何帮助都会有所帮助。 谢谢。
谢谢,我首先尝试了上下文、区域设置,但失败了,并且正在查看其他一些构造函数(我见过一个只提到区域设置的构造函数)。 无论如何,
它不起作用,因为我仍然得到: The method Geocoder(Context, Locale) is undefined for the type savemaplocation
I do: import android.location.Geocoder;
I am trying to get an address based on the long/lat. it appears that something like this should work?
Geocoder myLocation = Geocoder(Locale.getDefault());
List myList = myLocation.getFromLocation(latPoint,lngPoint,1);
The issue is that I keep getting : The method Geocoder(Locale) is undefined for the type savemaplocation
Any assistance would be helpful. Thank you.
Thanks, I tried the context, locale one first, and that failed and was looking at some of the other constructors (I had seen one that had mentioned just locale). Regardless,
It did not work, as I am still getting : The method Geocoder(Context, Locale) is undefined for the type savemaplocation
I do have : import android.location.Geocoder;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
以下代码片段正在为我做这件事(lat 和 lng 是在此位上方声明的双精度数):
The following code snippet is doing it for me (lat and lng are doubles declared above this bit):
下面是一个完整的示例代码,使用线程和处理程序在不阻塞 UI 的情况下获取地理编码器答案。
Geocoder 调用过程,可以位于 Helper 类中
这是在 UI Activity 中对此 Geocoder 过程的调用:
以及在 UI 中显示结果的处理程序:
不要忘记将以下权限放入
Manifest 中.xml
Here is a full example code using a Thread and a Handler to get the Geocoder answer without blocking the UI.
Geocoder call procedure, can be located in a Helper class
Here is the call to this Geocoder procedure in your UI Activity:
And the handler to show the results in your UI:
Don't forget to put the following permission in your
Manifest.xml
看起来这里发生了两件事。
1) 您在调用构造函数之前错过了
new
关键字。2) 您传递给 Geocoder 构造函数的参数不正确。 您正在传递一个需要
Context
的Locale
。有两个
Geocoder
构造函数,两者都需要Context
,其中一个还需要Locale
:解决方案
修改您的代码传入有效的上下文并包含
new
,您应该可以开始了。注意
如果您仍然遇到问题,则可能是权限问题。 地理编码隐式使用 Internet 来执行查找,因此您的应用程序将需要在清单中添加
INTERNET
use-permission 标记。在清单的
manifest
节点中添加以下使用权限节点。It looks like there's two things happening here.
1) You've missed the
new
keyword from before calling the constructor.2) The parameter you're passing in to the Geocoder constructor is incorrect. You're passing in a
Locale
where it's expecting aContext
.There are two
Geocoder
constructors, both of which require aContext
, and one also taking aLocale
:Solution
Modify your code to pass in a valid Context and include
new
and you should be good to go.Note
If you're still having problems it may be a permissioning issue. Geocoding implicitly uses the Internet to perform the lookups, so your application will require an
INTERNET
uses-permission tag in your manifest.Add the following uses-permission node within the
manifest
node of your manifest.原因是后端服务不存在:
The reason for this is the non-existent Backend Service:
首先使用 Location 和 LocationManager 类获取纬度和经度。 现在尝试使用下面的代码获取城市,地址信息
城市信息现在在 sb 中。 现在将 sb 转换为 String (使用 sb.toString() )。
First get Latitude and Longitude using Location and LocationManager class. Now try the code below for Get the city,address info
City info is now in sb. Now convert the sb to String (using sb.toString() ).
嗯,我还是很困惑。 所以这里有更多代码。
在离开地图之前,我调用
SaveLocation(myMapView,myMapController);
这就是最终调用我的地理编码信息的原因。但由于 getFromLocation 可以抛出 IOException ,我必须执行以下操作来调用 SaveLocation
然后我必须通过说它抛出 IOExceptions 来更改 SaveLocation :
并且每次都会崩溃。
Well, I am still stumped. So here is more code.
Before I leave my map, I call
SaveLocation(myMapView,myMapController);
This is what ends up calling my geocoding information.But since
getFromLocation
can throw anIOException
, I had to do the following to call SaveLocationThen I have to change SaveLocation by saying it throws IOExceptions :
And it crashes every time.
用这个
Use this