将位置转换为GeoPoint

4

大家好,我正在尝试获取用户的当前位置,并在地图上用覆盖层显示出来。我的问题是,用户的位置以位置形式呈现,而覆盖层数组只接受GeoPoints。以下是我尝试过的方法:

LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {      
            GeoPoint point = new GeoPoint.valueOf(location);
            OverlayItem overlayitem4 = new OverlayItem(point, "You Are Here", "Boulder, CO");               
        }

但是我在GeoPoint.valueOf(location);处遇到了错误,具体来说:

无法解析GeoPoint.valueOf的类型

所以我的问题是如何将位置信息转换为GeoPoint?感谢大家的时间。

2个回答

10

试一下这个

LocationListener locationListener = new LocationListener() {

     public void onLocationChanged(Location location) {      
         int lat = (int) (location.getLatitude() * 1E6);
         int lng = (int) (location.getLongitude() * 1E6);
         GeoPoint point = new GeoPoint(lat, lng);
         OverlayItem overlayitem4 = new OverlayItem(point, "You Are Here",   "Boulder, CO"); 
     }
}

这解决了我当前的问题,谢谢你,你会得到"勾号",但现在我无法通过 'itemizedoverlay.addOverlay(overlayitem4);' 添加它,因为数组声明在其范围外。有什么办法可以让 overlayitem4 在 onLocationsChanged 方法之外使用吗? - ninge
当然,您可以在任何地方声明OverlayItem4。但是,如果您需要将其用作全局变量,则必须在类的开头进行声明。 - Xenione
我尝试这样做:public class MainActivity extends MapActivity { OverlayItem currentP; 这样可以消除错误,但运行时出现了NullPointerException。我应该将其初始化为什么以开始? - ninge
不需要初始化,除非在赋值之前使用它。你能把你的代码发布到pastebin.com上吗?我可以看一下。 - Xenione
好的。我想我明白了。如果你不理解什么,提前抱歉我的英语,我会再次尝试解释。问题是你在onCreate方法中执行了itemizedoverlay.addOverlay(currentP)操作,但可能onLocationChanged方法没有触发,因为还没有获取到任何更新位置,这需要时间。所以你应该在onLocationChanged方法中(最后)添加这行代码itemizedoverlay.addOverlay(currentP)。 - Xenione

2

这对我起作用了,而且似乎更容易:

GeoPoint geoPoint = new GeoPoint(location);

1
这在 Firebase 版本 12.0.1 中测试后已不再起作用。 - Karan Modi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接