Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.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
如何在选择时更改javascript值_Javascript_Jquery_Google Maps - Fatal编程技术网

如何在选择时更改javascript值

如何在选择时更改javascript值,javascript,jquery,google-maps,Javascript,Jquery,Google Maps,当用户选择不同的城市时,我需要更改google地图的初始化坐标。当我选择一个选项时,地图的初始坐标需要更改 我的标记: <select class='form-control' id='id_district' type='text' name="district"> <option value='' disabled selected>District *</option> <option value="Limassol">Lim

当用户选择不同的城市时,我需要更改google地图的初始化坐标。当我选择一个选项时,地图的初始坐标需要更改

我的标记:

<select class='form-control' id='id_district' type='text' name="district">
    <option value='' disabled selected>District *</option>
    <option value="Limassol">Limassol</option>
    <option value="Nicosia">Nicosia</option>
    <option value="Paphos">Paphos</option>
    <option value="Larnaca">Larnaca</option>
    <option value="Ammoxostos">Ammoxostos</option>
</select>

此函数需要使用jQuery进行更改

当选择更改时,您需要调用地理编码器(而不是反向地理编码器):

<select class='form-control' id='id_district' type='text' name="district" onchange="geocodeAddress(this.value);">

function geocodeAddress(address) {
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
      if (results[0].geometry.viewport) {
        map.fitBounds(results[0].geometry.viewport);
      } else if (results[0].geometry.bounds) {
        map.fitBounds(results[0].geometry.bounds);
      } else {
        map.setCenter(results[0].geometry.location);
      }
      if (marker && marker.setMap) {
        marker.setMap(null);
      }
      marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}
html,
身体{
身高:100%;
保证金:0;
填充:0;
}
#地图画布{
身高:100%;
}

地区*
利马索尔
尼科西亚
帕福斯
拉纳卡
阿莫克索斯托斯

这可能有助于使用Geocoder,但最好对lat/lng进行静态解析并替换-
<select class='form-control' id='id_district' type='text' name="district" onchange="geocodeAddress(this.value);">

function geocodeAddress(address) {
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
      if (results[0].geometry.viewport) {
        map.fitBounds(results[0].geometry.viewport);
      } else if (results[0].geometry.bounds) {
        map.fitBounds(results[0].geometry.bounds);
      } else {
        map.setCenter(results[0].geometry.location);
      }
      if (marker && marker.setMap) {
        marker.setMap(null);
      }
      marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}