Javascript 在更改谷歌地图位置时遇到问题

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

当我试图改变位置时,我遇到了一个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("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);
}
参考:


如果每个数字都作为字符串传递,我可以确认会出现“图像损坏或截断”错误(因此DOM提取的字符串需要parseFloat)。

检查浏览器控制台,您看到哪些JavaScript错误?[14:37:06.243]图像损坏或截断:@[14:37:06.258]图像损坏或截断:@当我尝试时:警报(city_1); rezult是Nan,Nan有趣,我希望谷歌方面能更好地处理错误(例如抛出“无效纬度”之类的错误),但很好:这和这个问题有什么关系?@ShadowWizard你现在看到了吗?如果是,请重新考虑你的否决票!实际上,看到OP对这个问题的评论似乎不需要对数值进行解析,
新的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);
}