2012-07-25 107 views
-1

在这里,我有以下代码,但有错误。有人可以帮忙吗?android - 获取lon和lat的当前位置并放入GeoPoint中?

public void onLocationChanged(Location loc) { 
     // TODO Auto-generated method stub 
     GeoPoint point = new GeoPoint(loc.getLatitude(),loc.getLongitude()); 
     OverlayItem overlayitem = new OverlayItem(point, "My Current Location", "My Current Location"); 
     overlay.addOverlay(overlayitem); 
     mapOverlays.add(overlay); 

    } 
+0

提到的错误吗? – jeet 2012-07-25 10:29:27

+0

@jeet GeoPoint需要整数,但loc.getLatitude()返回一个双精度值 – 2012-07-25 10:30:54

+0

至少LogCat ... – overbet13 2012-07-25 10:46:13

回答

6

其实,GeoPoint坐标存储在微度,所以Location转换为GeoPoint你会使用此代码:

GeoPoint point = new GeoPoint((int)(loc.getLatitude() * 1E6), (int)(loc.getLongitude() * 1E6)); 

希望这有助于

1

我使用以下用度和/或微度创建GeoPoints:

/** 
* Create a {@link GeoPoint} from float values. 
* @param lat Latitude as float 
* @param lng Longitude as float 
* @return {@link GeoPoint} 
*/ 
public static GeoPoint createGeoPoint(double lat, double lng) { 
     return new GeoPoint(degreesToMicrodegrees(lat), degreesToMicrodegrees(lng)); 
} 

/** Convert a float point degree value into a micro deree value */ 
public static int degreesToMicrodegrees(double deg) { 
     return (int)(deg * 1E6); 
} 

这应该对你有所帮助。

所以,你的代码将是这样的:

public void onLocationChanged(Location loc) { 
    // TODO Auto-generated method stub 
    GeoPoint point = createGeoPoint(loc.getLatitude(),loc.getLongitude()); 
    OverlayItem overlayitem = new OverlayItem(point, "My Current Location", "My Current Location"); 
    overlay.addOverlay(overlayitem); 
    mapOverlays.add(overlay); 

} 
+0

运行时出现意外错误,但谢谢。 – 2012-07-25 10:45:47

+0

考虑到它是手写的:D – Moss 2012-07-25 11:04:32

+0

非常感谢您的帮助:) – 2012-07-25 12:23:49

0
GeoPoint pointRabat = new GeoPoint(microDegres(latitude), 
       microDegres(longitude)); 
     mapController.setCenter(pointRabat); 


    private int microDegres(double value) { 
     return (int) (value * 1000000); 
    }