javascript - Google Maps API v3: is there any function to check if a coordinate is inside a province or country - Stack Overflow

I want to allow the user to introduce the coordinates himself (or herself). The following code works:&l

I want to allow the user to introduce the coordinates himself (or herself). The following code works:

<head>
    <script src=".exp&sensor=false" type="text/javascript"></script>

    <script type="text/javascript" >

    var geocoder;
    var map;

    function paintCoordsInMap() {
      geocoder = new google.maps.Geocoder();
      var lat = document.getElementById('lat').value;
      var lng = document.getElementById('lng').value;
      var latlng = new google.maps.LatLng(lat, lng);
      var mapOptions = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    }

    </script>

    (...)
</head>


<body>
    (...)
    <f:field bean="alojamientoInstance" property="lat"/>
    <f:field bean="alojamientoInstance" property="lng"/>
    <button type='button' class="btn btn-primary" onclick="paintCoordsInMap()"> find coordinates in map! </button>  
    (...)
</body>

I want to check if the coordinates are inside a province (or country, for example) to launch a windows alert if not. If not, I'll draw a square in the map containing the province (or country), copy the coordenates of the corners. A code like the following should work (more or less):

if (lat > 10.111 || lat < 30.1213 || lng < 40.234 || lat > 60.23423) {
    alert('it is outside the country/province!');
}

I want to allow the user to introduce the coordinates himself (or herself). The following code works:

<head>
    <script src="http://maps.googleapis./maps/api/js?v=3.exp&sensor=false" type="text/javascript"></script>

    <script type="text/javascript" >

    var geocoder;
    var map;

    function paintCoordsInMap() {
      geocoder = new google.maps.Geocoder();
      var lat = document.getElementById('lat').value;
      var lng = document.getElementById('lng').value;
      var latlng = new google.maps.LatLng(lat, lng);
      var mapOptions = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    }

    </script>

    (...)
</head>


<body>
    (...)
    <f:field bean="alojamientoInstance" property="lat"/>
    <f:field bean="alojamientoInstance" property="lng"/>
    <button type='button' class="btn btn-primary" onclick="paintCoordsInMap()"> find coordinates in map! </button>  
    (...)
</body>

I want to check if the coordinates are inside a province (or country, for example) to launch a windows alert if not. If not, I'll draw a square in the map containing the province (or country), copy the coordenates of the corners. A code like the following should work (more or less):

if (lat > 10.111 || lat < 30.1213 || lng < 40.234 || lat > 60.23423) {
    alert('it is outside the country/province!');
}
Share Improve this question edited Sep 8, 2013 at 18:04 geocodezip 161k14 gold badges226 silver badges255 bronze badges asked Sep 8, 2013 at 2:55 chelderchelder 3,8977 gold badges60 silver badges95 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

As an option , you could do reverse geocoding, like get the address fusing lat & lng, and check if the address matches your mentioned province, such as:

var geocoder = new google.maps.Geocoder(),
    latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({ 'latLng': latlng }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {                
                //get the required address & check
                console.log(results[1].formatted_address); 
            }
        } else {
            //failed
        }
    });

If you have the coordinates of the borders of the province or country you want to check against, you can us the google.maps.geometry.poly.containsLocation method to determine whether the coordinates entered are inside the polygon.

containsLocation(point:LatLng, polygon:Polygon) | boolean | Computes whether the given point lies inside the specified polygon.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742305692a4418894.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信