Question

I use google map and just for representation of my work as a demo I need to throw some markers in the map I use the following code :

  var southWest = bounds.getSouthWest();
            var northEast = bounds.getNorthEast();
            var lngSpan = northEast.lng() - southWest.lng();
            var latSpan = northEast.lat() - southWest.lat();
                 tmplan=new google.maps.LatLng(
                            southWest.lat() + latSpan * Math.random(),
                            southWest.lng() + lngSpan * Math.random());

Everything is good but I want those markers to be shown that are in the territory of United state Does anyone has any suggestion?

Was it helpful?

Solution

First create a polygon of the US

var polygonUs = [
    new google.maps.LatLng(25.774252, -80.190262),
    new google.maps.LatLng(18.466465, -66.118292),
    new google.maps.LatLng(32.321384, -64.75737)
];

check if tmplan is inside polygon, if yes show it.

if (google.maps.geometry.poly.containsLocation(tmplan, polygonUs)) {
    marker = new google.maps.Marker({
        map: map,
        position: tmplan
    });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top