我希望使用Android的val
方法来计算两种不同对象类型之间的距离。我使用distanceTo()
LatLng
,因为它可以添加到Google地图上的object
位置。
marker
当我将位置对象类型添加到.position()时,会导致错误,因为它们不兼容。
我声明了一个位置 LatLng clontarfFC = new LatLng(53.369615, -6.1859571);
mMap.addMarker(new MarkerOptions().position(clontarfFC).title("Clontarf FC").snippet("St.Annes Park, Clontarf"));
,以便代表该应用用户的当前位置。
object
我试图找到两者之间的距离,如下:
Location currentLocation = new Location ("Curent Location");
currentLocation.setLatitude(53.3419322);
currentLocation.setLongitude(-6.2670159);
答案 0 :(得分:0)
据我所知,没有内置的方法,你可以创建一个简单但彻底的转换器,像这样(其中latLng是你的LatLng对象):
Location location = new Location("location_name");
location.setLatitude(latLng .latitude);
location.setLongitude(latLng .longitude);
location.setTime(new Date().getTime());
要转换回LatLng,请使用以下命令:
LatLng newLatLng = new LatLng(location.getLatitude(),
location.getLongitude());
答案 1 :(得分:0)
在gradle文件中添加以下依赖项
compile 'com.google.maps.android:android-maps-utils:0.4+'
在下面的代码行中实现并传递“start LatLng”和“End LatLng”来获取两个对象之间的距离
double distanceBetween = SphericalUtil.computeDistanceBetween(
beginLatLng, endLatLng);
答案 2 :(得分:0)
如所说fuzzKity你可以做LatLng position = new LatLng(loc.getLatitude(), loc.getLongitude());
你可以做那样的事情
float[] results = new float[3];
Location.distanceBetween(coordStart.latitude, coordStart.longitude, position.latitude, position.longitude, results);
double dist = results[0];