Google Maps - plot lines and distance (as the crow flies) between multiple latitude longitude pairs
Asked Answered
I

4

5

I'm looking to plot a route "as the crow flies" on a map via multiple latitude longitude pairs. I also need to calculate the total distance (and preferrably distance between points).

I have found this example http://www.daftlogic.com/projects-advanced-google-maps-distance-calculator.htm but it is far more complicated than I require - I don't need any kind of search functionality, as I already have my latitude longitude data. However, the way it displays the route is what I'm looking to achieve

Are there any simpler examples out there that I can build upon or any general guidance on achieving this?

Inhibitor answered 10/3, 2011 at 22:35 Comment(0)
C
6

Plotting a route "as the crow flies" is easy, just set polyline's geodesic option to true:

var geodesic = new google.maps.Polyline({geodesic: true});

You can use this example: http://code.google.com/apis/maps/documentation/javascript/examples/geometry-headings.html

To calculate the distance, you can utilize geometry library of the Google Maps API V3. http://code.google.com/apis/maps/documentation/javascript/geometry.html

The calculation is simple:

var distance = google.maps.geometry.spherical.computeDistanceBetween(latLngOfPointA, latLngOfPointB);

To compute the length of a path you can use computeLength function:

var path = geodesic.getPath();
var length = google.maps.geometry.spherical.computeLength(path)
Cuckooflower answered 11/3, 2011 at 8:40 Comment(0)
A
2

Be sure to include the geometry library in your call to Google Maps API.

Bootstrap from Documentation

<script type="text/javascript"       src="http://maps.google.com/maps/api/js?libraries=geometry&sensor=true_or_false">  </script>
Atalanti answered 30/6, 2011 at 22:38 Comment(0)
M
0

I have an Excel file with Vincenty's formula I could forward. Accuracy is better than 20 cm on 20'000 km.

Menke answered 3/1, 2012 at 13:41 Comment(0)
B
0

Through the Java Google Maps SDK, you can get the lat/lng for a specific address, then using the haversine distance formula to get the distance in KM.

    <dependency>
        <groupId>com.google.maps</groupId>
        <artifactId>google-maps-services</artifactId>
        <version>2.2.0</version>
    </dependency>

Code:

public static Double getDistanceInKmAsTheCrowFlies(String apiKey, String address1, String address2)
{
    GeocodingResult[] address1GeoCode = getGeocoding(apiKey, address1);
    GeocodingResult[] address2GeoCode = getGeocoding(apiKey, address2);

    if (address1GeoCode != null && address2GeoCode != null)
    {
        double lat1 = address1GeoCode[0].geometry.location.lat;
        double lng1 = address1GeoCode[0].geometry.location.lng;

        double lat2 = address2GeoCode[0].geometry.location.lat;
        double lng2 = address2GeoCode[0].geometry.location.lng;

        return getHaversineDistance(lat1, lng1, lat2, lng2);
    }
    return null;

}

public static GeocodingResult[] getGeocoding(String apiKey, String address)
{

    GeocodingApiRequest req = GeocodingApi.newRequest(new GeoApiContext.Builder()
            .apiKey(apiKey)
            .build());

    GeocodingResult[] query = req.address(address).awaitIgnoreError();

    if (query != null && query.length > 0)
    {
        return query;
    }
    return null;
}

public static double getHaversineDistance(double lat1, double lng1, double lat2, double lng2)
{
    int r = 6371; // average radius of the earth in km
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lng2 - lng1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
            * Math.sin(dLon / 2) * Math.sin(dLon / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double d = r * c;
    return d;
}
Bleachers answered 9/2 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.