DedalusNine
DedalusNine

Reputation: 47

pass values Database to Javascript

I need to pass values longitude and latitude stored in the database trough GetLocation method and pass them to javascript, how can I do this?

DATABASE TABLE Location

LocationId
Latitude
Longitude
Name

BUSSINES CLASS METHOD

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);
}

Upvotes: 0

Views: 34

Answers (1)

Carl Edwards
Carl Edwards

Reputation: 14434

With data-attributes you could insert the values from your backend as so. It doesn't have to be the #map div per se but just using it as a basic example:

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

And then retrieve them from the javascript end using 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);
}

Upvotes: 1

Related Questions