2017-08-05 33 views
0

我试着检查给定的poiont是否在Rectangle或外部如果是内部返回true,或者如果是外部返回false,并且我想写入输出到文本框。 我有我的代码 map.htmlJavascript谷歌地图包含矩形的点

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Simple Map</title> 
    <meta name="viewport" content="initial-scale=1.0"> 
    <meta charset="utf-8"> 
    <style> 
     /* Always set the map height explicitly to define the size of the div 
     * element that contains the map. */ 
     #map { 
     height: 100%; 
     } 
     /* Optional: Makes the sample page fill the window. */ 
     html, body { 
     height: 100%; 
     margin: 0; 
     padding: 0; 
     } 



     #floating-panel { 
     position: absolute; 
     top: 4px; 
     left: 9%; 
     z-index: 5; 
     background-color: #fff; 
     padding: 5px; 
     border: 1px solid #999; 
     text-align: center; 
     font-family: 'Roboto','sans-serif'; 
     line-height: 30px; 
     padding-left: 10px; 
     } 

    </style> 
    </head> 
    <body> 


    <div id="floating-panel"> 
     <input id="address" type="textbox" value="enter address"> 
     <input id="submit" type="button" value="Geocode"> 
     <input id="output" type="textbox" value="output"> 
    </div> 



    <div id="map"></div> 

    <script> 
     var map; 
     //-----This example adds a user-editable rectangle to the map. 
     function initMap() { 
     var map = new google.maps.Map(document.getElementById('map'), { 
      center: {lat: 45.065140, lng: 19.946804}, 
      zoom: 12 
     }); 

     /*------for geocoding(I give addres not lng lang)*/ 
     var geocoder = new google.maps.Geocoder(); 

     document.getElementById('submit').addEventListener('click', function() { 
      geocodeAddress(geocoder, map); 
     }); 
     /* ------------- */ 

     var bounds = { 
      north: 45.095140, 
      south: 45.025140, 
      east: 19.966804, 
      west: 19.926804 
     }; 
     // Define a rectangle and set its editable property to true. 
     var rectangle = new google.maps.Rectangle({ 
      bounds: bounds, 
      editable: true, 
      draggable: true, 
      geodesic: true 
     }); 
     rectangle.setMap(map); 

      document.getElementById("output").value = check_is_in_or_out(marker); 


     } 


     function check_is_in_or_out(marker){ 
     google.maps.event.addListener(map, 'bounds_changed', function() { 
     var bounds = map.getBounds().contains(marker.getPosition()); 
     }); 
    return bounds; 
} 
     /* ------------- */ 
     /*------for geocoding(I give addres not lng lang)*/ 
     var address; 
     var latitude; 
     var longitude; 
     var marker; 
     function geocodeAddress(geocoder, resultsMap) { 
     address = document.getElementById('address').value; 
     geocoder.geocode({'address': address}, function(results, status) { 
      if (status === 'OK') { 
      resultsMap.setCenter(results[0].geometry.location); 
      latitude = results[0].geometry.location.lat(); 
      longitude = results[0].geometry.location.lng(); 
      marker = new google.maps.Marker({ 
       map: resultsMap, 
       position: results[0].geometry.location 
      }); 
      } else { 
      alert('Geocode was not successful for the following reason: ' + status); 
      } 
     return marker; 
     }); 


     } 
     /* ------------- */ 
    </script> 
    <script src="http://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap" 
    async defer></script> 
    </body> 
</html> 

,我想如果给点让我在输出true的内部或得到false如果外 那么,我该怎么办呢?野老,没有什么happend当我使用:document.getElementById("output").value = check_is_in_or_out(marker); * API_KEY如果你还没有API KEY使用这个:http://maps.google.com/maps/api/js?sensor=false

编辑 感谢geocodezip您的帮助。现在我的代码是:

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Map2</title> 
    <meta name="viewport" content="initial-scale=1.0"> 
    <meta charset="utf-8"> 
    <style> 
     /* Always set the map height explicitly to define the size of the div 
     * element that contains the map. */ 
     #map { 
     height: 100%; 
     } 
     /* Optional: Makes the sample page fill the window. */ 
     html, body { 
     height: 100%; 
     margin: 0; 
     padding: 0; 
     } 



     #floating-panel { 
     position: absolute; 
     top: 4px; 
     left: 9%; 
     z-index: 5; 
     background-color: #fff; 
     padding: 5px; 
     border: 1px solid #999; 
     text-align: center; 
     font-family: 'Roboto','sans-serif'; 
     line-height: 30px; 
     padding-left: 10px; 
     } 

    </style> 
    </head> 
    <body> 


    <div id="floating-panel"> 
     <input id="address" type="textbox" value="Dobrodol"> 
     <input id="submit" type="button" value="Geocode"> 
     <input id="output" type="textbox" value="output"> 
    </div> 



    <div id="map"></div> 

    <script> 
    function check_is_in_or_out(marker) { 
    var insideRectangle = false; 
    if (rectangle && rectangle.getBounds && marker && marker.getPosition()) 
    insideRectangle = rectangle.getBounds().contains(marker.getPosition()); 

    return insideRectangle; 
} 

var map; 
var rectangle; 
function initMap() { 
    var map = new google.maps.Map(document.getElementById('map'), { 
    center: { 
     lat: 45.065140, 
     lng: 19.946804 
    }, 
    zoom: 12 
    }); 

    var geocoder = new google.maps.Geocoder(); 

    document.getElementById('submit').addEventListener('click', function() { 
    geocodeAddress(geocoder, map); 
    }); 
    var bounds = { 
    north: 45.095140, 
    south: 45.025140, 
    east: 19.966804, 
    west: 19.926804 
    }; 
    // Define a rectangle and set its editable property to true. 
    rectangle = new google.maps.Rectangle({ 
    bounds: bounds, 
    editable: true, 
    draggable: true, 
    geodesic: true 
    }); 
    rectangle.setMap(map); 


} 
var address; 
var latitude; 
var longitude; 
var marker; 

function geocodeAddress(geocoder, resultsMap) { 
    address = document.getElementById('address').value; 
    geocoder.geocode({ 
    'address': address 
    }, function(results, status) { 
    if (status === 'OK') { 
     resultsMap.setCenter(results[0].geometry.location); 
     latitude = results[0].geometry.location.lat(); 
     longitude = results[0].geometry.location.lng(); 
     marker = new google.maps.Marker({ 
     map: resultsMap, 
     position: results[0].geometry.location 
     }); 
     document.getElementById("output").value = check_is_in_or_out(marker); 
    } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
    } 
    }); 
} 
    </script> 
    <script src="http://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap" 
    async defer></script> 
    </body> 
</html> 

而正是在geocodeAddress(geocoder, resultsMap)精细我们使用document.getElementById("output").value = check_is_in_or_out(marker);和我的textBox改变为真或假。但是,我怎样才能将这个值设置为var?像这样:var bool = check_is_in_or_out(marker);在布尔我会有真或假? 我试过在geocodeAddress中使用返回,但我的变量是未定义的,所以在我看来,我只能在内部函数geocoder.geocode中做到这一点,但我想这个变量全局不是本地的。

+0

如果你想检测是否标记是一个矩形,你为什么用克地图的边界? (或者是你想要使用的矩形?)另外,“bounds_changed”事件监听器是异步的,该函数不会返回有用的结果。 – geocodezip

+0

@geocodezip所以我应该用什么来代替bounds_changed? – JavaCoder

回答

2

更改check_is_in_or_out使用编辑矩形的界限contains检查:

function check_is_in_or_out(marker){ 
    var insideRectangle = false; 
    if (rectangle && rectangle.getBounds && marker && marker.getPosition()) 
    insideRectangle = rectangle.getBounds().contains(marker.getPosition()); 

    return insideRectangle; 
} 

调用它,每当标记位置或矩形范围的更改(如果你想查地图边界是不相关的如果它位于可编辑的矩形内)。

proof of concept fiddle

代码片段:

function check_is_in_or_out(marker) { 
 
    var insideRectangle = false; 
 
    if (rectangle && rectangle.getBounds && marker && marker.getPosition()) 
 
    insideRectangle = rectangle.getBounds().contains(marker.getPosition()); 
 

 
    return insideRectangle; 
 
} 
 

 
var map; 
 
var rectangle; 
 
function initMap() { 
 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
    center: { 
 
     lat: 45.065140, 
 
     lng: 19.946804 
 
    }, 
 
    zoom: 12 
 
    }); 
 

 
    var geocoder = new google.maps.Geocoder(); 
 

 
    document.getElementById('submit').addEventListener('click', function() { 
 
    geocodeAddress(geocoder, map); 
 
    }); 
 
    var bounds = { 
 
    north: 45.095140, 
 
    south: 45.025140, 
 
    east: 19.966804, 
 
    west: 19.926804 
 
    }; 
 
    // Define a rectangle and set its editable property to true. 
 
    rectangle = new google.maps.Rectangle({ 
 
    bounds: bounds, 
 
    editable: true, 
 
    draggable: true, 
 
    geodesic: true 
 
    }); 
 
    rectangle.setMap(map); 
 

 
    document.getElementById("output").value = check_is_in_or_out(marker); 
 

 
    google.maps.event.addListener(rectangle, 'bounds_changed', function() { 
 
    document.getElementById("output").value = check_is_in_or_out(marker); 
 
    }); 
 
} 
 
var address; 
 
var latitude; 
 
var longitude; 
 
var marker; 
 

 
function geocodeAddress(geocoder, resultsMap) { 
 
    address = document.getElementById('address').value; 
 
    geocoder.geocode({ 
 
    'address': address 
 
    }, function(results, status) { 
 
    if (status === 'OK') { 
 
     resultsMap.setCenter(results[0].geometry.location); 
 
     latitude = results[0].geometry.location.lat(); 
 
     longitude = results[0].geometry.location.lng(); 
 
     marker = new google.maps.Marker({ 
 
     map: resultsMap, 
 
     position: results[0].geometry.location 
 
     }); 
 
     document.getElementById("output").value = check_is_in_or_out(marker); 
 
    } else { 
 
     alert('Geocode was not successful for the following reason: ' + status); 
 
    } 
 
    }); 
 
}
html, 
 
body, 
 
#map { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<div id="floating-panel"> 
 
    <input id="address" type="textbox" value="Dobrodol"> 
 
    <input id="submit" type="button" value="Geocode"> 
 
    <input id="output" type="textbox" value="output"> 
 
</div> 
 
<div id="map"></div> 
 

 
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap" async defer></script>

+0

非常感谢,这对我非常有用! @geocodezip maybye你知道我现在怎么能得到这个布尔值真或假的Java类?使用javafx和WebEngine引擎= mapWebView.getEngine(); ? – JavaCoder

+0

我编辑我的帖子,因为我现在有一个问题。请参阅我的编辑。 – JavaCoder

+0

这是一个新的,不同的问题 – geocodezip