2016-01-28 32 views

回答

0
function landOrWater(lat, lng, callback) { 
    var map_url = "http://maps.googleapis.com/maps/api/staticmap?center="+lat+","+lng+"&zoom="+map.getZoom()+"&size=1x1&maptype=roadmap" 
    var canvas = document.createElement('canvas'); 
    var ctx = canvas.getContext('2d'); 

    var image = new Image(); 
    image.crossOrigin = "Anonymous"; // dope hack 
    image.src = map_url; 

    image.onload = function() { 
     canvas.width = image.width; 
     canvas.height = image.height; 
     canvas.getContext('2d').drawImage(image, 0, 0, image.width, image.height); 
     var pixelData = canvas.getContext('2d').getImageData(0, 0, 1, 1).data; 
     if(pixelData[0] > 160 && pixelData[0] < 181 && pixelData[1] > 190 && pixelData[1] < 210) { 
      result = "water"; 
     } else { 
      result = "land"; 
     } 
     callback(result); 
    } 
} 

landOrWater(somelat, somelong, function(result){ 
    console.log(result); 
}); 
+0

您可以添加一些解釋嗎? – Veve

+0

上面的問題清楚地解釋了這個問題。該代碼是一個完整的解決方案。 – chrisallick