Google地图上的标记文字会引发错误JavaScript运行时错误:InvalidValueError:setTitle:不是字符串

时间:2014-02-12 10:34:01

标签: javascript google-maps-api-3

我有点卡在以下代码中,这会引发此错误:

0x800a138f - JavaScript runtime error: Unable to get property 'lat' of undefined or null reference

我的服务器端代码返回数据,但是当我到达这一行时:

var myLatlng = new window.google.maps.LatLng(data.lat, data.lng);

它抛出了上面的错误,任何一个戴着新鲜眼睛的人都可以看到我做错了什么。

<script type="text/javascript">
    var markers = [
     @foreach (var item in Model.DisplayLocationsFound)
           {
         <text>
               "title"  ['@Regex.Replace(@item.FullAddress(),"<br/>"," ")'],
               "lat"    ['@item.Latitude'],
               "lng"    ['@item.Longitude'],
               </text>
           }
    ];
</script>
<script type="text/javascript">

    window.onload = function () {
        var mapOptions = {
            center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
            zoom: 8,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };


        var infoWindow = new google.maps.InfoWindow();
        var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
        var latLng = new Array();
        for (i = 0; i < markers.length; i++) {
            var data = markers[i];
            var myLatlng = new google.maps.LatLng(data.lat, data.lng);
            latLng.push(myLatlng);
            var marker = new StyledMarker({
                styleIcon: new StyledIcon(StyledIconTypes.BUBBLE,
                                { color: "2590BA", text: data.title }),
                position: myLatlng,
                map: map,
                title: data.title
            });
            (function (marker, data) {
                google.maps.event.addListener(marker, "click", function (e) {
                    infoWindow.setContent(data.description);
                    infoWindow.open(map, marker);
                });
            })(marker, data);
        }

    }
</script>

这是从foreach循环看数据的方式

var markers = [

           "title"['London,City Of London, Greater London'],
           "lat"['51.517'],
           "lng"['-0.106'],


           "title"['London,Ontario'],
           "lat"['42.983'],
           "lng"['-81.250'],


           "title"['London,Orange Walk'],
           "lat"['17.983'],
           "lng"['-88.433'],


           "title"['London,Litoral'],
           "lat"['2.283'],
           "lng"['9.800'],


           "title"['London,'],
           "lat"['1.983'],
           "lng"['-157.467'],


           "title"['London,Kachin State'],
           "lat"['25.100'],
           "lng"['96.283'],


           "title"['London,Adamawa'],
           "lat"['5.717'],
           "lng"['5.783'],


           "title"['London,General Santos'],
           "lat"['6.010'],
           "lng"['125.129'],


           "title"['London,Altaisky Krai'],
           "lat"['52.800'],
           "lng"['79.550'],


           "title"['London,Mpumalanga'],
           "lat"['-24.767'],
           "lng"['30.867'],

        ];

点是,如果我硬编码值,它可以正常工作

更新

将代码更改为:

<script type="text/javascript">
        var markers = [
        @foreach (var item in Model.DisplayLocationsFound)
    {
        <text>
                  @*"title"  ['@Regex.Replace(@item.FullAddress(),"<br/>"," ")'],
                  "lat"    ['@item.Latitude'],
                  "lng"['@item.Longitude'],*@

                  ['@Regex.Replace(@item.FullAddress(),"<br/>",",")' ,@item.Latitude, @item.Longitude],


                  </text>
    }
        ];
    </script>
    <script type="text/javascript">

        window.onload = function () {

            var mapOptions = {
                center: new window.google.maps.LatLng(markers[1], markers[2]),
                zoom: 8,
                mapTypeId: window.google.maps.MapTypeId.ROADMAP
            };


            var infoWindow = new window.google.maps.InfoWindow();
            var map = new window.google.maps.Map(document.getElementById("dvMap"), mapOptions);
            var latLng = new Array();
            for (var i = 0; i < markers.length; i++) {
                var data = markers[i];
                var myLatlng = new window.google.maps.LatLng(markers[1], markers[2]);
                latLng.push(myLatlng);
                var marker = new StyledMarker({
                    styleIcon: new StyledIcon(StyledIconTypes.BUBBLE,
                                    { color: "2590BA", text: markers[0] }),
                    position: myLatlng,
                    map: map,
                    title: markers[0]
                });
                (function (marker, data) {
                    window.google.maps.event.addListener(marker, "click", function (e) {
                        infoWindow.setContent(data.description);
                        infoWindow.open(map, marker);
                    });
                })(marker, data);
            }

        }
    </script>

现在出错:

0x800a139e - JavaScript runtime error: InvalidValueError: setTitle: not a string

0 个答案:

没有答案