我有超过 136 个具有纬度和经度的位置,并且我正在尝试使用 java 脚本、剃须刀和 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>
}
}
我基本上只是遍历所有位置并获取纬度和经度,问题是,标记不显示,因为 for each 中的一段 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');
}