如何在google Maps API V2中从当前位置到纬度目的地获取里程/公里和分钟

时间:2013-01-21 20:10:35

标签: android google-maps

如何在Google地图API V2中获取里程/公里和负数从当前位置到纬度/经度目的地。如果我得到这个值,我想存储在textview中。

我的代码是:

import android.os.Bundle;

import com.actionbarsherlock.app.SherlockActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class DetailMapActivity extends SherlockActivity {
    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_detail_map);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
        map.setMyLocationEnabled(true);

        LatLng LOC_DESTINATION = new LatLng(-6.33438, 106.74316);

        map.addMarker(new MarkerOptions().position(LOC_DESTINATION)
                .title("Destination Title").snippet("Destination Description"));

        map.moveCamera(CameraUpdateFactory.newLatLngZoom(LOC_DESTINATION, 40));

        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

        String miles = "what this code?";
        Log.d("HaichalMap", miles);
    }
}

感谢您的回答。

3 个答案:

答案 0 :(得分:2)

使用Location

尝试此操作
Location locationA = new Location("Location A");
locationA.setLatitude(lat_a);
locationA.setLongitude(lon_a);

Location locationB = new Location("Location B");
locationB.setLatitude(lat_b);
locationB.setLongitude(lon_b);

int metres = Math.round(locationA.distanceTo(locationB)); // Returns in Metres

将米转换为所需的输出

答案 1 :(得分:0)

Google地图不提供路线说明。您需要查看Google Directions API

答案 2 :(得分:0)

您需要根据新旧坐标(纬度和经度)计算距离。这将以米为单位返回结果。然后,您需要将其转换为英里/公里。就初始和最终坐标的距离计算而言,任何THIS线程如何对你有所帮助。希望能回答你的问题。