Verify if a point is Land or Water in Google Maps

后端 未结 18 1866
不知归路
不知归路 2020-11-22 17:16

..and then Google-maps \"divide the waters from the waters\"

Well, not in the biblical sense but..

I would like to know what options I have in order to verif

18条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 17:57

    Here's another example in pure JavaScript: http://jsfiddle.net/eUwMf/

    As you can see, the ideia is basically the same as rebe100x, getting the image from Google static map API, and read the first pixel:

    $("#xGps, #yGps").change(function() {
        var img = document.getElementById('mapImg');
    
        // Bypass the security issue : drawing a canvas from an external URL.
        img.crossOrigin='anonymous';
    
        var xGps = $("#xGps").val();
        var yGps = $("#yGps").val();
    
        var mapUrl = "http://maps.googleapis.com/maps/api/staticmap?center=" + xGps + "," + yGps +
            "&zoom=14&size=20x20&maptype=roadmap&sensor=false";
    
        // mapUrl += "&key=" + key;
    
        $(img).attr("src", mapUrl);
    
        var canvas = $('')[0];
        canvas.width = img.width;
        canvas.height = img.height;
        canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height);
    
        var pixelData = canvas.getContext('2d').getImageData(1, 1, 1, 1).data;
    
        if (pixelData[0] == 164 &&
            pixelData[1] == 190 &&
            pixelData[2] == 220) {
            $("#result").html("Water");
        } else {
            $("#result").html("Not water");
        }
    });
    

提交回复
热议问题