Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
谷歌地图API:使用javascript和html接受相同半径的搜索地址_Javascript_Jquery_Html_Css_Google Maps - Fatal编程技术网

谷歌地图API:使用javascript和html接受相同半径的搜索地址

谷歌地图API:使用javascript和html接受相同半径的搜索地址,javascript,jquery,html,css,google-maps,Javascript,Jquery,Html,Css,Google Maps,我想要实现的是在内部接受相同半径的客户端地址,如果该地址不具有相同半径,则应进行验证(“地址未接受”),否则该地址将不起作用 当客户或用户在radius内输入相同地址的地址时,应进行验证(“接受”) 当客户或用户输入的地址与radius内的地址不一致时,应进行验证(“不接受”) 下面是我尝试执行的代码:- <html> <title></title> <head></head> <script src="https://ajax

我想要实现的是在内部接受相同半径的客户端地址,如果该地址不具有相同半径,则应进行验证(“地址未接受”),否则该地址将不起作用

  • 当客户或用户在radius内输入相同地址的地址时,应进行验证(“接受”)

  • 当客户或用户输入的地址与radius内的地址不一致时,应进行验证(“不接受”)

  • 下面是我尝试执行的代码:-

     <html>
    <title></title>
    <head></head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load("maps", "3",{other_params:"key="});
      </script>
      <script type="text/javascript">
      var map = null;
      var radius_circle;
      var markers_on_map = [];
      var geocoder;
      var infowindow;
    
      //all_locations is just a sample, you will probably load those from database
       var all_locations = [
      {type: "Restaurant", name: "Restaurant 1", lat: 40.723080, lng: -73.984340},
      {type: "School", name: "School 1", lat: 40.724705, lng: -73.986611},
      {type: "School", name: "School 2", lat: 40.724165, lng: -73.983883},
      {type: "Restaurant", name: "Restaurant 2", lat: 40.721819, lng: -73.991358},
      {type: "School", name: "School 3", lat: 40.732056, lng: -73.998683}
      ];
    
       //initialize map on document ready
       $(document).ready(function(){
       var latlng = new google.maps.LatLng(40.723080, -73.984340); //you can use any location as center 
       on map startup
       var myOptions = {
        zoom: 1,
        center: latlng,
        mapTypeControl: true,
        mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
        navigationControl: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
        };
       map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
       geocoder = new google.maps.Geocoder();
         google.maps.event.addListener(map, 'click', function(){
           if(infowindow){
             infowindow.setMap(null);
             infowindow = null;
           }
         });
        });
    
      function showCloseLocations() {
      var i;
      var radius_km = $('#radius_km').val();
      var address = $('#address').val();
    
      //remove all radii and markers from map before displaying new ones
       if (radius_circle) {
        radius_circle.setMap(null);
        radius_circle = null;
       }
      for (i = 0; i < markers_on_map.length; i++) {
        if (markers_on_map[i]) {
            markers_on_map[i].setMap(null);
            markers_on_map[i] = null;
        }
    }
    
    if (geocoder) {
        geocoder.geocode({'address': address}, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
                    var address_lat_lng = results[0].geometry.location;
                    radius_circle = new google.maps.Circle({
                        center: address_lat_lng,
                        radius: radius_km * 1000,
                        clickable: false,
                        map: map
                    });
                    if (radius_circle) map.fitBounds(radius_circle.getBounds());
                    for (var j = 0; j < all_locations.length; j++) {
                        (function (location) {
                            var marker_lat_lng = new google.maps.LatLng(location.lat, location.lng);
                            var distance_from_location = google.maps.geometry.spherical.computeDistanceBetween(address_lat_lng, marker_lat_lng); //distance in meters between your location and the marker
                            if (distance_from_location <= radius_km * 1000) {
                                var new_marker = new google.maps.Marker({
                                    position: marker_lat_lng,
                                    map: map,
                                    title: location.name
                                });                                     google.maps.event.addListener(new_marker, 'click', function () {
                                    if(infowindow){
             infowindow.setMap(null);
             infowindow = null;
           }
                                    infowindow = new google.maps.InfoWindow(
            { content: '<div style="color:red">'+location.name +'</div>' + " is " + distance_from_location + " meters from my location",
              size: new google.maps.Size(150,50),
              pixelOffset: new google.maps.Size(0, -30)
            , position: marker_lat_lng, map: map});
                                });
                                markers_on_map.push(new_marker);
                            }
                        })(all_locations[j]);
                    }
                } else {
                    alert("No results found while geocoding!");
                }
            } else {
                alert("Geocode was not successful: " + status);
            }
        });
    }
      }
    </script>
    <body style="margin:0px; padding:0px;" >
    <input id="address" value="Second Steet, New York" placeholder="Input Address"/>
     <select id="radius_km">
     <option value=1>1km</option>
     <option value=2>2km</option>
     <option value=5>5km</option>
     <option value=30>30km</option>
     </select>
     <button onClick="showCloseLocations()">Show Locations In Radius</button>
     <div id="map_canvas"  style="width:500px; height:300px;">
     </body>
     </html>
    
    
    load(“maps”,“3”,“其他参数:”key=“});
    var-map=null;
    var半径_圆;
    变量标记在映射上的值=[];
    var地理编码器;
    var信息窗口;
    //所有位置都只是一个示例,您可能会从数据库加载这些位置
    变量所有位置=[
    {类型:“餐厅”,名称:“餐厅1”,lat:40.723080,液化天然气:-73.984340},
    {类型:“学校”,名称:“学校1”,lat:40.724705,lng:-73.986611},
    {类型:“学校”,名称:“学校2”,lat:40.724165,lng:-73.983883},
    {类型:“餐厅”,名称:“餐厅2”,纬度:40.721819,液化天然气:-73.991358},
    {类型:“学校”,名称:“学校3”,lat:40.732056,lng:-73.998683}
    ];
    //在文档准备就绪时初始化映射
    $(文档).ready(函数(){
    var latlng=new google.maps.latlng(40.723080,-73.984340);//您可以使用任何位置作为中心
    地图启动
    变量myOptions={
    缩放:1,
    中心:拉特林,
    mapTypeControl:true,
    mapTypeControlOptions:{style:google.maps.MapTypeControlStyle.DROPDOWN_MENU},
    导航控制:对,
    mapTypeId:google.maps.mapTypeId.ROADMAP
    };
    map=new google.maps.map(document.getElementById(“map_canvas”),myOptions);
    geocoder=新的google.maps.geocoder();
    google.maps.event.addListener(映射,'click',函数(){
    如果(信息窗口){
    infowindow.setMap(空);
    infowindow=null;
    }
    });
    });
    函数showCloseLocations(){
    var i;
    var radius_km=$('#radius_km').val();
    var address=$('#address').val();
    //在显示新半径和标记之前,请从地图中删除所有半径和标记
    if(半径×圆){
    圆半径设置图(空);
    圆的半径=零;
    }
    对于(i=0;i如果(与位置的距离尝试将
    showCloseLocations
    功能替换为以下功能:

      function showCloseLocations() {
        var i;
        var radius_km = $('#radius_km').val();
        var address = $('#address').val();
    
        for (i = 0; i < markers_on_map.length; i++) {
          if (markers_on_map[i]) {
            markers_on_map[i].setMap(null);
            markers_on_map[i] = null;
          }
        }
    
        if (geocoder) {
          geocoder.geocode({ 'address': address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
    
                var address_lat_lng = results[0].geometry.location;
    
                if (radius_circle) {
                  radius_circle.setRadius(radius_km * 100);
                }
                else {
                  radius_circle = new google.maps.Circle({
                    center: address_lat_lng,
                    radius: radius_km * 1000,
                    clickable: false,
                    map: map
                  });
                }
    
                if (!radius_circle.getBounds().contains(address_lat_lng)) {
                  alert("This address is outside radius.");
                  return;
                }
    
                if (radius_circle) map.fitBounds(radius_circle.getBounds());
    
                for (var j = 0; j < all_locations.length; j++) {
                  (function(location) {
                    var marker_lat_lng = new google.maps.LatLng(location.lat, location.lng);
                    var distance_from_location = google.maps.geometry.spherical.computeDistanceBetween(address_lat_lng, marker_lat_lng); //distance in meters between your location and the marker
                    if (distance_from_location <= radius_km * 1000) {
                      var new_marker = new google.maps.Marker({
                        position: marker_lat_lng,
                        map: map,
                        title: location.name
                      });
                      google.maps.event.addListener(new_marker, 'click', function() {
                        if (infowindow) {
                          infowindow.setMap(null);
                          infowindow = null;
                        }
                        infowindow = new google.maps.InfoWindow({
                          content: '<div style="color:red">' + location.name + '</div>' + " is " + distance_from_location + " meters from my location",
                          size: new google.maps.Size(150, 50),
                          pixelOffset: new google.maps.Size(0, -30),
                          position: marker_lat_lng,
                          map: map
                        });
                      });
                      markers_on_map.push(new_marker);
                    }
                  })(all_locations[j]);
                }
              }
              else {
                alert("No results found while geocoding!");
              }
            }
            else {
              alert("Geocode was not successful: " + status);
            }
          });
        }
      }
    
    函数showCloseLocations(){
    var i;
    var radius_km=$('#radius_km').val();
    var address=$('#address').val();
    对于(i=0;i如果(从位置到位置的距离)您试图做什么,但发布的代码不做?此代码不会验证Radius内的位置,因此您指的是用户键入的位于“纽约第二大街”之外的任何地址不应被接受为有效输入?我也不清楚问题出在哪里。@evan是的,正确。这就是我想要的。您可以查看名为laundryapp的应用程序,了解他们是如何使用地址的。我认为这是我目前需要的完美示例。@evan