0

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);
}
3
  • What about storing them as data-attributes in the DOM? Commented Nov 28, 2016 at 1:55
  • Are you using a framework like AngularJS or is this straight javascript? Commented Nov 28, 2016 at 1:57
  • no framework, just javascript Commented Nov 28, 2016 at 2:02

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.