创建 Android 位置对象

发布于 2024-10-16 14:40:51 字数 553 浏览 4 评论 0原文

我正在使用 Geocoder 类通过以下代码获取多个位置对象:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
   List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 3);
   Address[] addresses_array = new Address[addresses.size()];

    addresses.toArray(addresses_array);
    for( int i = 0; i < addresses_array.length; i++ ){
       //create location object here
       locOBJ.setLatitude(LATITUDE);
       locOBJ.setLongitude(LONGITUDE);
    }

此外,在 forloop 内,我尝试动态创建位置对象以添加到数组中;

如何创建空白位置对象?

I am using the Geocoder class to fetch multiple location objects by using the following code:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
   List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 3);
   Address[] addresses_array = new Address[addresses.size()];

    addresses.toArray(addresses_array);
    for( int i = 0; i < addresses_array.length; i++ ){
       //create location object here
       locOBJ.setLatitude(LATITUDE);
       locOBJ.setLongitude(LONGITUDE);
    }

In addition, inside the forloop, I am trying to dymanically create location objects to add to an array;

How can I create blank location objects ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪歌 2024-10-23 14:40:51

假设您引用 android.location.Location 使用构造函数,该构造函数接受提供程序字符串并将其设置为您想要的任何内容。

Assuming you refer to android.location.Location use the constructor which takes a provider string and set it to whatever you want.

笑着哭最痛 2024-10-23 14:40:51

这并不是它的真正用途,如果您想在谷歌地图上绘制内容,您可能需要查看 GeoPoint 类。处理 Map OverlayItem 对象。您打算如何处理 Location 对象?此外,您还应该在线程或 AsyncTask 中执行 getFromLocation 调用,因为它正在执行远程服务器调用。

使用 GeoPoint 类。

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 3);
int size = addresses.size();
GeoPoint gp[] = new GeoPoint[size];
for(int i = 0; i<size; i++) {
   Address addr = addresses.get(i);
   gp[i] = new GeoPoint(addr.getLatitude()*1000000, 
                        address.getLongitude()*1000000);
}

这些值为 * 1000000,因为 GeoPoint 需要 E6 值。还要意识到,如果没有匹配项,则数组的长度可能为 0。

That's not really what it's intended for, if you are looking to plot stuff on a google map you may want to look into the GeoPoint class. You must use the GeoPoint class when dealing with Map OverlayItem objects. What do you plan to do with the Location objects? Also you should do the getFromLocation call in a thread or AsyncTask since it is doing a remote server call.

using the GeoPoint class.

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 3);
int size = addresses.size();
GeoPoint gp[] = new GeoPoint[size];
for(int i = 0; i<size; i++) {
   Address addr = addresses.get(i);
   gp[i] = new GeoPoint(addr.getLatitude()*1000000, 
                        address.getLongitude()*1000000);
}

The values are * 1000000 because the GeoPoint wants E6 values. Also realize that if there are no matches the array may be length 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文