【问题标题】:Convert Location to GeoPoint将位置转换为地理点
【发布时间】:2012-07-29 17:01:47
【问题描述】:

所有-我试图获取用户的当前位置,而不是在地图上显示带有覆盖项的位置。我的问题是用户的位置是作为一个位置进来的,而覆盖数组只接受 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? 谢谢大家的宝贵时间。

【问题讨论】:

    标签: android google-maps location overlay


    【解决方案1】:

    试试这个

    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);' 添加它因为数组是在其范围之外声明的。关于如何在 onLocationsChanged 方法之外获取 overlayitem4 的任何想法?
    • 当然你可以在任何地方声明overlayitem4。如果您需要将其用作全局变量,则必须在课程开始时声明
    • 我试图这样做: public class MainActivity extends MapActivity { OverlayItem currentP;这消除了错误,但是当我运行它时,我得到了 NullPointerException。我应该将它初始化为什么开始?
    • 不,你不需要初始化,除非你在赋值之前使用它。你能把你的代码贴在 pastebin.com 上,我可以看看
    • 好的。我想我明白了。如果你不理解一些对不起可能英语的事情,我会再次尝试解释。问题是你这样做 itemizedoverlay.addOverlay(currentP);在 onCreate 方法上,但可能 onLocationChanged 方法不会触发,因为没有获得任何更新位置,但它需要时间。所以你应该做的是添加这一行 itemizedoverlay.addOverlay(currentP);在 onLocationChanged 里面(最后)。
    【解决方案2】:

    这对我有用,而且似乎更容易:

    GeoPoint geoPoint = new GeoPoint(location);
    

    【讨论】:

    • 这在 firebase 版本 12.0.1 中不再适用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多