在更改Google地图位置时遇到问题

use*_*873 3 html javascript google-maps

当我尝试更改位置时,我遇到了问题.我已连接到地图

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);
Run Code Online (Sandbox Code Playgroud)

现在我需要在地图上更改位置

<select onChange="changeCity()" id="city"><option value="50.4501, 30.523400000000038">????</option><option value="49.839683, 24.029717000000005">?????</option></select>
Run Code Online (Sandbox Code Playgroud)

为此,我正在使用:

function changeCity() {
   var city = document.getElementById('city').value;
   var city_1 = new google.maps.LatLng(city);
   map.setCenter(city_1);
}
Run Code Online (Sandbox Code Playgroud)

但没有任何反应(我看到灰色区域).请帮忙...!

ppe*_*rka 6

问题是

somefunction("xx.xxxxxxx, xx.xxxxxxx");
Run Code Online (Sandbox Code Playgroud)

不等于

somefunction(xx.xxxxxxx, xx.xxxxxxx);
Run Code Online (Sandbox Code Playgroud)

您在option标记中的值被指定为单个字符串实例,如"50.4501, 30.523400000000038"基辅.请注意双引号.

new LatLng()构造期望2个数字.因此,您必须将值字符串解析为单独的纬度和经度值,并将它们传递给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);
}
Run Code Online (Sandbox Code Playgroud)

参考: