在Android Studio上有一种简单的方法可以获得两个标记之间的距离和时间吗?

时间:2017-03-19 20:52:40

标签: android location google-maps-markers google-distancematrix-api

以下是我的某个地点的“地图活动”。在代码的底部,我试图计算两个标记之间的距离。因此,当我运行它时,它将出现在地图上。有没有一种简单的方法可以做到这一点,因为我不会这样做。感谢

package com.example.aids.gettingtherepage;

import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

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

public class FromBelfastCity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_from_belfast_city);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng BelfastCity = new LatLng(54.6176, -5.8718);
        LatLng BelfastCityCentre = new LatLng(54.607868, -5.926437);
        mMap.addMarker(new MarkerOptions().position(BelfastCity).title("Belfast City Airport"));
        mMap.addMarker(new MarkerOptions().position(BelfastCityCentre).title("Belfast City Centre"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(BelfastCity));


        float[] results = new float[1];
        Location.distanceBetween(BelfastCity.latitude, BelfastCity.longitude,
                BelfastCityCentre.latitude, BelfastCityCentre.longitude,
                results);
    }
}

这是下面的输出:

这两个标记在谷歌地图上绘制。但是它们没有连接,没有方向距离/时间。

The output I get on the emulator is here

0 个答案:

没有答案