Javascript 在更改谷歌地图位置时遇到问题
当我试图改变位置时,我遇到了一个liitle问题。我连接到地图Javascript 在更改谷歌地图位置时遇到问题,javascript,html,google-maps,Javascript,Html,Google Maps,当我试图改变位置时,我遇到了一个liitle问题。我连接到地图 var mapOptions = { zoom: 7, center: new google.maps.LatLng(48.379433, 31.165579999999977), mapTypeControl: false, mapTypeId: google.maps.MapTypeId.ROADMAP } map = new google.maps.Map(document.getElementById
var mapOptions = {
zoom: 7,
center: new google.maps.LatLng(48.379433, 31.165579999999977),
mapTypeControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
现在我需要改变地图上的位置
<select onChange="changeCity()" id="city"><option value="50.4501, 30.523400000000038">Київ</option><option value="49.839683, 24.029717000000005">Львів</option></select>
但什么也没发生(我看到了灰色地带)。请帮忙 问题是
somefunction("xx.xxxxxxx, xx.xxxxxxx");
不等于
somefunction(xx.xxxxxxx, xx.xxxxxxx);
选项
标记中的值被指定为单个字符串实例,如基辅的“50.4501,30.5234000000000038”
。注意双引号
new LatLng()
构造函数需要两个数值。因此,您必须将值字符串解析为单独的纬度和经度值,并将它们传递给new LatLng()
构造函数:
function changeCity() {
var cityString = document.getElementById('city').value;
//value string is split into two parts by the ',' character
var arrayOfValues = cityString.split(',');
//parse latitude string part to a floating point value (first part)
var lat = parseFloat(arrayOfValues[0]);
//parse longitude string part to a floating point value (second part)
var lon = parseFloat(arrayOfValues[1]);
//create new LatLng instance with correct values
var city_1 = new google.maps.LatLng(lat, lon);
map.setCenter(city_1);
}
参考:
新的google.maps.LatLng(arrayOfValues[0],arrayOfValues[1])
可能会起到同样的作用,但解析时最好执行一些自定义验证,以防某些值无效。@ShadowWizard在查找,我不知道。。。它将这两个参数都引用为数字,但我不知道,如果它不尝试一些启发式方法来转换为数字,如果它被赋予字符串。。。
function changeCity() {
var cityString = document.getElementById('city').value;
//value string is split into two parts by the ',' character
var arrayOfValues = cityString.split(',');
//parse latitude string part to a floating point value (first part)
var lat = parseFloat(arrayOfValues[0]);
//parse longitude string part to a floating point value (second part)
var lon = parseFloat(arrayOfValues[1]);
//create new LatLng instance with correct values
var city_1 = new google.maps.LatLng(lat, lon);
map.setCenter(city_1);
}