Razor Syntax没有像我预期的那样工作

时间:2013-05-09 15:18:05

标签: javascript razor google-maps-api-3

我拥有经纬度超过136个的位置,我正在尝试使用java脚本,razor和C#创建一个好的地图上的136个地图标记。问题是,剃刀中的JavaScript片段没有显示出来,如果我正确地做了一切,我很好奇。

    var mapMarker;

    @{
        IEnumerable<UFA.Location.Core.Location> Location = UFALocation___Front.Helpers.QueryHelper.QueryHelper.getAllLocations();
        foreach(var loc in Location){
            <text>
                mapMarker = new google.maps.Marker({
                    position: new google.maps.LatLng(@loc.latitude, @loc.longitude),
                    map: map,
                    title: "Hello World!"
                });
            </text>
        }
    }

我基本上只是循环遍历所有位置并获得纬度和经度,问题是,标记不会显示,因为每个位置的javascript片段都不存在。

完整的剧本:

function success(position) {
        var s = document.querySelector('#status');

        if (s.className == 'success') {
            // not sure why we're hitting this twice in FF, I think it's to do with a cached result coming back    
            return;
        }

        s.innerHTML = "found you!";
        s.className = 'label label-success';


        var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        var directionsService = new google.maps.DirectionsService();
        var directionsDisplay = new google.maps.DirectionsRenderer();

        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 15,
            center: latlng,
            mapTypeControl: false,
            navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL },
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            title: "You are here! (at least within a " + position.coords.accuracy + " meter radius)"
        });

        var mapMarker;

        @{
            IEnumerable<UFA.Location.Core.Location> Location = UFALocation___Front.Helpers.QueryHelper.QueryHelper.getAllLocations();
            foreach(var loc in Location){
                <text>
                    mapMarker = new google.maps.Marker({
                        position: new google.maps.LatLng(@loc.latitude, @loc.longitude),
                        map: map,
                        title: "Hello World!"
                    });
                </text>
            }
        }

        var SampleMarker = new google.maps.Marker({
            position: new google.maps.LatLng(51.379, -113.53),
            map: map,
            title: "Hello World!"
        }); 

        directionsDisplay.setMap(map);

        var request = {
            origin: latlng,
            destination: '304 304 26th ave sw calgary alberta',
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        };

        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }
        });
    }

    function error(msg) {
        var s = document.querySelector('#status');
        s.innerHTML = typeof msg == 'string' ? msg : "failed";
        s.className = 'alert alert-error';

        // console.log(arguments);
    }

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(success, error);
    } else {
        error('not supported');
    }

0 个答案:

没有答案