How do I read an array from JavaScript

0

I have an array generated from a MySql table through PHP, I want to use the script provided by Google to display on a map a marker from the latitude and longitude stored in the array. How would the procedure be, when I have the data of lat and long in $ array ['latitude'] and $ array ['length'] ???

<script>
    var map;
    var latitud = -38.858228;
    var longitud = -60.057083;
    function initMap() {
      map = new google.maps.Map(document.getElementById('map'), {
         center: {lat: latitud, lng: longitud},
         zoom: 18
        });
      }
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDxDngjvn6vXgmzU4oA5lSqDeyaC-OJ6sQ&callback=initMap"
async defer></script>
    
asked by Ricardo Pastuszek 07.10.2017 в 17:18
source

2 answers

0

In the code that you show at any time you are creating a marker, you are simply centering the map on the location you want to show, to add a marker you should do the following:

<script>
    var map;
    var latitud = $arreglo['latitud'];
    var longitud = $arreglo['longitud '];
    function initMap() {
      map = new google.maps.Map(document.getElementById('map'), {
         center: {lat: latitud, lng: longitud},
         zoom: 18
        });
      }

      var marker = new google.maps.Marker({
          position: {lat:parseFloat(latitud), lng: parseFloat(longitud )},
          map: map,
          title: 'Titulo del marcador'
       });
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDxDngjvn6vXgmzU4oA5lSqDeyaC-OJ6sQ&callback=initMap"
async defer></script>

I hope you serve, greetings!

    
answered by 09.10.2017 / 15:10
source
-3

It is very general what you ask, because depending on many variables the procedures would be different. In the case that the arrangement that you get from PHP is being directly displayed in your HTML, you could put it in some hidden HTML element, such as a:

<input id="miArregloP" hidden value="<?php echo json_encode(miArrgloDeMySQL); ?>"/>

Then that information in JSON format can be extracted with javascript in the following way:

var miArregloP = JSON.parse(document.getElementById('miArregloP').value);

And having the arrangement in javascript you can use it with your google API, without problems.

In case you want to consume a service through JavaScript, that's a longer story and I recommend you study it.

    
answered by 07.10.2017 в 19:34