2016-11-28 62 views
0

我需要传递数据库中存储的经纬度值,通过GetLocation方法传递给javascript,我该怎么做?将数据库传递给Javascript

数据库表中的位置

LocationId 
Latitude 
Longitude 
Name 

BUSSINES类方法

public List<Location> GetLocation() { return Bd.Location.ToList(); } 

JAVASCRIPT

function initMap() { 
var mapCanvas = document.getElementById("map"); 
var mapOptions = { 
    center: new google.maps.LatLng(**Latitude** , **Longitude**), <-- 
    zoom: 15 
}; 
var map = new google.maps.Map(mapCanvas, mapOptions); 
} 
+0

什么将它们存储数据 - 在DOM中的属性? –

+0

你使用的是AngularJS这样的框架,还是这个javascript? –

+0

没有框架,只是javascript – DedalusNine

回答

1

随着数据的属性,你可以从你的后端插入值左右。它不必是#map DIV本身,而是只是用它作为一个基本的例子:

<div id="map" data-longitude="<!-- longitude value goes here -->" data-latitude="<!-- latitude value goes here -->"> 
    ... 
</div> 

再从JavaScript的结束检索它们使用getAttribute

function initMap() { 
    var mapCanvas = document.getElementById("map"); 

    var latitude = mapCanvas.getAttribute("data-latitude"); 
    var longitude = mapCanvas.getAttribute("data-longitude"); 

    var mapOptions = { 
    center: new google.maps.LatLng(latitude, longitude), 
    zoom: 15 
    }; 

    var map = new google.maps.Map(mapCanvas, mapOptions); 
}