在google map中将动态变量中的空格格式化为连字符中的连字符

问题描述 投票:0回答:1

我正在尝试格式化并替换+ marker.title +输出中的空格,并将它们更改为超级( - )以动态打开模态。例如圣地亚哥转换为圣地亚哥。任何帮助将不胜感激。

不知道你需要看多少代码,但这是整个循环。谢谢。

    $count = count($output);
            $i=1;
            echo 'var locations = [';
                foreach($output as $o) {
                    if ($i == $count) { $c = ''; } else { $c = ','; } 
                    echo json_encode($o) . $c;
                    $i++; 
                } 
            echo '];';
        ?>
        var marker, i;
        var infowindow = new google.maps.InfoWindow();

        google.maps.event.addListener(map, 'click', function() {
            infowindow.close();
        });

     for (i = 0; i < locations.length; i++) {
       markericon = (locations[i][7] == 'undefined') ? '' : locations[i][7];

        marker = new google.maps.Marker({
            icon: markericon,
            position: new google.maps.LatLng(locations[i][5], locations[i][6]),
            map: map,
            title: locations[i][0],
        });

        google.maps.event.addListener(marker, 'click', (function(marker, i) {

            return function() {

                $('#'+marker.title+'').modal('show');

            }

        })(marker, i));
javascript php google-maps google-maps-markers
1个回答
0
投票

我知道你想修改这行JavaScript代码

$('#'+marker.title+'').modal('show');

为了用-替换空格,你应该使用String.replace()函数和regular expression一起用于空字符串/\s/。所以,你的代码应该改为

 ('#'+marker.title.replace(/\s/ig, "-")+'').modal('show');

概念证明:

var title = "San Diego";
document.getElementById("result").innerHTML = title.replace(/\s/ig, "-");
<div id="result">
</div>

我希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.