Last Updated: February 25, 2016
·
2.714K
· michalkow

Compensate for Google Maps inaccuracy

I recently got back form a trip to HangZhou. I wanted to make a small website, for me for me friends with some fun facts about our trip, pictures, and maps of path we traveled.
Last one turned out to be a little tricky.

I used Google Earth to trace the path of our journey. Saved as KML file, and then applied overlay to Google Map on my site.

That is when I realized satellite maps is fine, but road map is off by some distance. Turns out roads and labels form road map don’t always match actual roads on satellite map. I don’t know for what regions it happens to be like that, but HangZhou is one of them.

To fix that I copied coordinates of my path from KML file and putted them in txt file. I wrote small php script to offset the latitude and longitude by amount I determined would be correct for this particular case.

<?php
$myFile = "lat.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);

function newloc($matches) {
    $loc = (float) $matches[0];
    //For HangZhou I determined latitudes would be around 30 and longitudes around 120 
    if($loc>60) $loc+=0.0047;
    if($loc<60) $loc-=0.0024;
    return $loc;
}

echo preg_replace_callback('/[-+]?[0-9]*\.[0-9]*/', "newloc", $theData);

fclose($fh);
?>

I pasted the result to my KML file. Finally I just needed some javascript to change overlays as the map display changes from road map to satellite or the other way around.

var overlays = [ 
    new google.maps.KmlLayer('LINK_TO_MY_KLM_FOR_ SATELLITE.kml'), 
    new google.maps.KmlLayer('LINK_TO_MY_KLM_FOR_ROADMAP.kml')
];

overlays[1].setMap(map);    

google.maps.event.addListener(map, 'maptypeid_changed', function() {
    if(map.getMapTypeId()=='roadmap') {
        overlays[0].setMap(null);
        overlays[1].setMap(map);    
    } else {
        overlays[1].setMap(null);
        overlays[0].setMap(map);    
    }
});