最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论