Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing google maps API v3
    primarykey
    data
    text
    <p>I am trying to get the distance between two location using Google Mps API following <a href="https://stackoverflow.com/questions/10906805/finding-streets-get-their-coordinates-and-calculating-distances-between-marker">this post</a>. I also found the <a href="http://jsfiddle.net/foxwisp/vQGMr/1/" rel="nofollow noreferrer">following example</a>.</p> <p>I have integrated the same code and but I don't see any results. What am I doing wrong here?</p> <p>Can anyone please let me know of any links where the examples have been indicated for Google Maps APIv3?</p> <pre><code> &lt;html&gt; &lt;head&gt; &lt;style&gt; h1{ font-family:arial,helvetica,sans-serif; font-size:20px; font-weight:bold; } p{ font-family:arial,helvetica,sans-serif; } &lt;/style&gt; &lt;script include="/home/rajeev/Desktop/ge/jquery-1.7.2.js"&gt;&lt;/script&gt; &lt;script&gt; var map; var marker, marker2; function initialize() { var mapDiv = document.getElementById('map-canvas'); map = new google.maps.Map(mapDiv, { center: new google.maps.LatLng(-36.86501268451408, 174.723858833313), zoom: 16, mapTypeId: google.maps.MapTypeId.ROADMAP }); } $(function() { initialize(); var geocoder = new google.maps.Geocoder(); //Geocode the first address geocoder.geocode({ address : '27 West View Road Westmere Auckland New Zealand', region: 'no' }, function(results, status){ //Create the first marker marker = new google.maps.Marker({ position: new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()), map: map, }); //Now geocode the second address geocoder.geocode({ address : '37 Old Mill Road Westmere Auckland New Zealand', region: 'no' }, function(results, status){ //Create the second marker marker2 = new google.maps.Marker({ position: new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()), map: map, }); //Now create the line (we have both markers at this point guaranteed) var line = new google.maps.Polyline({ path: new Array(marker.getPosition(),marker2.getPosition()), strokeColor: '#ff0000', strokeOpacity: 1.0, strokeWeight: 2 }); line.setMap(map); //Now calculate the distance var R = 6371; // km var dLat = (marker.getPosition().lat() - marker2.getPosition().lat()).toRad(); var dLon = (marker.getPosition().lng()- marker2.getPosition().lng()).toRad(); var lat1 = marker2.getPosition().lat().toRad(); var lat2 = marker.getPosition().lat().toRad(); var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); var distance = Math.ceil(R * c * 1000); $('p').html('The distance between the two addresses is:' + distance +'m'); }); }); }); if (typeof(Number.prototype.toRad) === "undefined") { Number.prototype.toRad = function() { return this * Math.PI / 180; } } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Distance polyline between two street addresses&lt;/h1&gt; &lt;hr/&gt; &lt;p&gt; &lt;/p&gt; &lt;hr/&gt; &lt;div id="map-canvas" style="width:500px;height:500px"&gt;&lt;/div&gt; &lt;/body&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload