0

我正在使用 Google Maps API v3 (javascript)。如果模型中存在坐标(.NET MVC 4),我需要在地图上放置标记。坐标可以正确查看,我知道这是因为地图正确居中和缩放。问题是 - 我不能使用函数marker.setPosition();,在 javascript 控制台中我得到错误:

类型错误:a.Xa 未定义

...c){4>a.A++?c?aee(b):aej(b):ab=h;ad||(ad=re(aF))}oJ[H].j=function() {这个....

我的代码:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&sensor=false&language=@UICulture.Substring(0, UICulture.IndexOf("-"))"></script>
<script>
    function initialize() {
        var mapOptions = {
            center: new google.maps.LatLng('@Model.CoordinatesLatitude' != '0.0' ? '@Model.CoordinatesLatitude' : 55.169438,
                                            '@Model.CoordinatesLongitude' != '0.0' ? '@Model.CoordinatesLongitude' : 23.881275),
            zoom: '@Model.CoordinatesLatitude' != '0.0' ? 17 : 7,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);

        var input = document.getElementById('searchMap');
        var autocomplete = new google.maps.places.Autocomplete(input);

        autocomplete.bindTo('bounds', map);

        var infowindow = new google.maps.InfoWindow();
        var marker = new google.maps.Marker({
            map: map
        });
        
        if ('@Model.CoordinatesLatitude' != '0.0' && '@Model.CoordinatesLongitude' != '0.0') {
            var position = new google.maps.LatLng(parseFloat('@Model.CoordinatesLatitude'), parseFloat('@Model.CoordinatesLongitude'));
            console.log(position);
            var img = {
                size: new google.maps.Size(71, 71),
                origin: new google.maps.Point(0, 0),
                anchor: new google.maps.Point(17, 34),
                scaledSize: new google.maps.Size(35, 35)
            };
            marker.setIcon(img);
            marker.setPosition(position);
            marker.setVisible(true);
        }

        google.maps.event.addListener(autocomplete, 'place_changed', function () {
            infowindow.close();
            marker.setVisible(false);
            var place = autocomplete.getPlace();
            if (!place.geometry) {
                return;
            }

            if (place.geometry.viewport) {
                map.fitBounds(place.geometry.viewport);
            } else {
                map.setCenter(place.geometry.location);
                map.setZoom(17);
            }
            var image = {
                url: place.icon,
                size: new google.maps.Size(71, 71),
                origin: new google.maps.Point(0, 0),
                anchor: new google.maps.Point(17, 34),
                scaledSize: new google.maps.Size(35, 35)
            };
            marker.setIcon(image);
            marker.setPosition(place.geometry.location);
            marker.setVisible(true);

            var address = '';
            if (place.address_components) {
                address = [
                    (place.address_components[0] && place.address_components[0].short_name || ''),
                    (place.address_components[1] && place.address_components[1].short_name || ''),
                    (place.address_components[2] && place.address_components[2].short_name || '')
                ].join(' ');
                $('#coordLat').val(place.geometry.location.lat());
                $('#coordLng').val(place.geometry.location.lng());
                $('#ExternalId').val(place.id);
            }
            infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
            infowindow.open(map, marker);
        });

    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>

我已经调试了我的代码并且坐标是正确的。不管我是否使用 parseFloat,都会抛出同样的错误。如果我使用 constructor 和 set position: position,它会抛出相同的错误。

4

1 回答 1

0

我已经解决了这个问题(将坐标放在中心位置,然后在中心设置标记):

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&sensor=false&language=@UICulture.Substring(0, UICulture.IndexOf("-"))"></script>
<script>
    function initialize() {
        var cnt = new google.maps.LatLng(55.169438, 23.881275);
        var zoom = 7;
        if ('@Model.CoordinatesLatitude' != '0.0' && '@Model.CoordinatesLongitude' != '0.0') {
            var latitude = parseFloat('@Model.CoordinatesLatitude');
            var longitude = parseFloat('@Model.CoordinatesLongitude');
            cnt = new google.maps.LatLng(latitude, longitude);
            zoom = 17;
        }
        var mapOptions = {
            center: cnt,
            zoom: zoom,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);

        var input = document.getElementById('searchMap');
        var autocomplete = new google.maps.places.Autocomplete(input);

        autocomplete.bindTo('bounds', map);

        var infowindow = new google.maps.InfoWindow();
        var marker = new google.maps.Marker({
            map: map
        });

        if ('@Model.CoordinatesLatitude' != '0.0' && '@Model.CoordinatesLongitude' != '0.0') {
            marker.setPosition(cnt);
        }

        google.maps.event.addListener(autocomplete, 'place_changed', function () {
            infowindow.close();
            marker.setVisible(false);
            var place = autocomplete.getPlace();
            if (!place.geometry) {
                return;
            }

            if (place.geometry.viewport) {
                map.fitBounds(place.geometry.viewport);
            } else {
                map.setCenter(place.geometry.location);
                map.setZoom(17);
            }
            var image = {
                url: place.icon,
                size: new google.maps.Size(71, 71),
                origin: new google.maps.Point(0, 0),
                anchor: new google.maps.Point(17, 34),
                scaledSize: new google.maps.Size(35, 35)
            };
            marker.setIcon(image);
            marker.setPosition(place.geometry.location);
            marker.setVisible(true);

            var address = '';
            if (place.address_components) {
                address = [
                    (place.address_components[0] && place.address_components[0].short_name || ''),
                    (place.address_components[1] && place.address_components[1].short_name || ''),
                    (place.address_components[2] && place.address_components[2].short_name || '')
                ].join(' ');
                $('#coordLat').val(place.geometry.location.lat());
                $('#coordLng').val(place.geometry.location.lng());
                $('#ExternalId').val(place.id);
            }
            infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
            infowindow.open(map, marker);
        });

    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>
于 2013-05-24T08:09:12.763 回答