JavaScript Google Maps API and Google Maps location shows different location in PHP google maps google maps,google maps google sheets,google maps

I am integrating my client website with Google APIs, and I have a task to validate the user address by using the user's postcode (UK), and to get the address, I have used https://ideal-postcodes.co.uk/.

So, when a user enter a postcode, then an ajax request is sent to this postcode URL with the postcode the user entered. And it returns a set of addresses from that postcode, and the user selects one of them.

Now, this postcode URL also returns the lat and long for that particular postcode. And for the next step, I have used the Google Maps API, which shows direction, distance, and route from source to destination.

That all works fine, but for some postcodes, it shows the wrong location.

When I check that postcode in Google Maps, it shows fine; hence there is no issue with lat & long. There is instead something else which I have been yet unable to find.

Please help me out guys. I have tried searching and using many different ways of coding, but still it remains same.

Here is my JS code, in which I have calculated multiple distances.

var map;
var latc;
var lonc;
var latd;
var lond;
var latr;
var lonr;
var directionsService;
var directionsDisplay;
var orgin;
var destinations;
var midway;
var waypoints = [];
var service;
var stat = false;
function initMap()
{
    map = null;
    waypoints=[];
    directionsDisplay = null;
    directionsService = null;
    orgin = "";
    destinations = "";
    latc = $("#lat").val();
    lonc = $("#lon").val();
    latd = $("#latd").val();
    lond = $("#lond").val();
    lonr = $("#lonr").val();
    latr = $("#latr").val();
    orgin = {lat: parseFloat(latc), lng: parseFloat(lonc)};
    if(latr==="0" && lonr==="0")
    {
        destinations = {lat: parseFloat(latd), lng: parseFloat(lond)};
    }
    else
    {
        stat = true;
        waypoints.push({
            location: new google.maps.LatLng(parseFloat(latd),parseFloat(lonr)),
            stopover:true
        });
        midway = {lat: parseFloat(latd), lng: parseFloat(lonr)};
        destinations = {lat: parseFloat(latr), lng: parseFloat(lonr)};
    }
    console.log(orgin);
    console.log(destinations);
    directionsService = new google.maps.DirectionsService;
    directionsDisplay = new google.maps.DirectionsRenderer;
    map = new google.maps.Map(document.getElementById('map'), {
        center: orgin,
        zoom: 17,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    directionsDisplay.setMap(map);
    calculateAndDisplayRoute(directionsService, directionsDisplay);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay)
{
        directionsService.route(
            {
                origin: orgin,
                destination: destinations,
                waypoints: waypoints,
                durationInTraffic: true,
                optimizeWaypoints: true,
                provideRouteAlternatives: true,
                avoidHighways: false,
                avoidTolls: false,
                travelMode: google.maps.TravelMode.DRIVING
            },
            function(response, status)
            {
                if (status === google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setDirections(response);
                    distanceandduration();
                }
                else
                {
                    window.alert('Directions request failed due to ' + status);
                }
            }
        );
}
function distanceandduration()
{
    if(latr==="0" && lonr==="0")
    {
        var a = new Array(orgin);
        var b = new Array(destinations);
    }
    else
    {
        var a = new Array(orgin,midway);
        var b = new Array(midway,destinations);
    }
    service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix({
        origins: a,
        destinations: b,
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.METRIC,
        durationInTraffic: true,
        avoidHighways: false,
        avoidTolls: false
    }, function (response, status) {
        if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
            var distnumber = [];
            var durationnumber = [];
            var dvDistance = document.getElementById("showde");
            dvDistance.innerHTML = "";
            dvDistance.innerHTML = "<div class='row-fluid'><div class='control-group span12'><div class='controls'>";
            for(var i=0;i<response.rows.length;i++)
            {
                var distance = "";
                var duration = "";
                if(i===0)
                {}else{
                    dvDistance.innerHTML += "<br/>";
                }
                distnumber[i] = Math.round(parseInt(response.rows[i].elements[i].distance.value)/1000);
                durationnumber[i] = rectime(response.rows[i].elements[i].duration.value);
                console.log(response.rows[i].elements[i].duration);
                distance += response.rows[i].elements[i].distance.text;
                duration += response.rows[i].elements[i].duration.text;
                dvDistance.innerHTML += "<h2>Route "+(i+1)+"</h2>";
                dvDistance.innerHTML += "Distance: " + distance + "<br />";
                dvDistance.innerHTML += "Duration:" + duration;
                dvDistance.innerHTML += "<hr>";
            }
            dvDistance.innerHTML +="</div></div></div>";
            if(latr==="0" && lonr==="0")
            {
                enquirydetailreport(distnumber,durationnumber,'oneway');
            }
            else
            {
                enquirydetailreport(distnumber,durationnumber,'return');
            }
        } else {
            alert("Unable to find the distance via road.");
        }
    });
}
function rectime(sec) {
    var hr = Math.floor(sec / 3600);
    var min = Math.floor((sec - (hr * 3600))/60);
    sec -= ((hr * 3600) + (min * 60));
    sec += ''; min += '';
    while (min.length < 2) {min = '0' + min;}
    while (sec.length < 2) {sec = '0' + sec;}
    hr = (hr)?hr:'00';
    return hr+ ':' + min + ':' + sec;
}

And note that this postcode issue occurs only for a few specific postcodes:

Example : Source : CW10 0QF
Waypoint : G78 1EP
Destination : CW10 0QF

Any help is appreciated. Thank you.

Answer:1

I was having some trouble when trying to sort and group the array items in JavaScript. Here is the sample inputs: var arr = [ {merchantName: '', branchName: 'e', branchAddress: '', total: 10.5}, ]; ...

I was having some trouble when trying to sort and group the array items in JavaScript. Here is the sample inputs: var arr = [ {merchantName: '', branchName: 'e', branchAddress: '', total: 10.5}, ]; ...

  1. javascript array sorting algorithm
  2. javascript array sorting methods
  3. javascript array sorting objects
  4. javascript array sorting
  5. javascript array sorting function
  6. javascript array sorting numbers
  7. javascript array sorting library
  8. javascript array sorting example
  9. javascript array sorting by key
  10. javascript array sorting date
  11. javascript array sort by property
  12. javascript array sort by value
  13. javascript array sort alphabetically
  14. javascript array sort string
  15. javascript array sort ascending
  16. javascript array sort by object property
  17. javascript array sort reverse
  18. javascript array sort desc
  19. javascript array sort not working
  20. javascript array sort compare function

We have multiple lists on the page that can have hundreds of items so for performance reasons, we'd like to use one-time bindings for things and only update when we know it needs to be updated and ...

We have multiple lists on the page that can have hundreds of items so for performance reasons, we'd like to use one-time bindings for things and only update when we know it needs to be updated and ...

Counting the items contained in an object, I used to have: Object.keys(state.posts).length Now, I have changed my posts state into an immutablejs Map, like so: Map({ "1": {title: "First Title"}, "2"...

Counting the items contained in an object, I used to have: Object.keys(state.posts).length Now, I have changed my posts state into an immutablejs Map, like so: Map({ "1": {title: "First Title"}, "2"...

I have the following radio buttons : <td valign="top"> <label>Have you ever been tested?</label> <input required id="Test" name="Test" type="radio" class="...

I have the following radio buttons : <td valign="top"> <label>Have you ever been tested?</label> <input required id="Test" name="Test" type="radio" class="...

  1. check radio button jquery
  2. check radio button javascript
  3. check radio button selected
  4. check radio button value
  5. check radio button html
  6. check radio button selected jquery
  7. check radio button js
  8. check radio button value jquery
  9. check radio button based on value jquery
  10. check radio button by default
  11. check radio button with keyboard
  12. check radio button selected c#
  13. check radio button is selected in selenium
  14. check radio button value in javascript
  15. check radio button on div click
  16. check radio button checked
  17. check radio button programmatically android
  18. check radio button jquery by id
  19. check radio button android
  20. check radio button jquery by name