Problem - Add Google Maps bookmarks from JSON

Asked

Viewed 208 times

0

I have a problem that is the following: I have to make a call from a JSON that contains latitude and longitude of several locations and then add them to a Google Maps map as markers. I can upload both the map and JSON, but the markers are not displayed for an unknown reason. Like, the code doesn’t show any errors in the Google Chrome log, but the bookmarks don’t want to appear. Does anyone know how to fix this? Probably the error is in the syntax, but I don’t know where...

NOTE: JSON I cannot show, because it is not mine, but of a company. But the structure of JSON is correct.

Look at the code below...

Javasctipt (jQuery)

// js/gmap_index.js

var mapOptions;
var mapa;
var geocoder;

function CallFirstMap () {
  mapOptions = {
    center: new google.maps.LatLng(-29.1634031, -51.1796683),
    zoom: 8,
    mapTypeId: google.maps.MapTypeId.HYBRID,
    scaleControl: true,
    scaleControlOptions: {
      position: google.maps.ControlPosition.BOTTOM_RIGHT
    },
    streetViewControl: true,
    streetViewControlOptions: {
      position: google.maps.ControlPosition.RIGHT_BOTTOM
    },
    mapTypeControl: true,
    mapTypeControlOptions: {
      position: google.maps.ControlPosition.RIGHT_BOTTOM
    }
  };
  mapa = new google.maps.Map(document.getElementById("map"), mapOptions);
  geocoder = new google.maps.Geocoder();
}

// js/callPop.js

$(document).ready (function () {
  $.getJSON("my.json", function (data) {
    var marcador, localMarcador;
    $.each(data, function (i, arq) {
      localMarcador = new google.maps.LatLng(arq.lat, arq.long);
      marcador = new google.maps.Marker({
        map: mapa,
        position: localMarcador
      });
    });
  });
});

HTML:

<body>

    <div id="map"></div>
    <script src="https://maps.googleapis.com/maps/api/js?key=my_key&callback=CallFirstMap" async defer></script> <!-- coloquei a key como my_key para evitar o uso indevido -->
    <script src="js/gmap_index.js"></script>

  • In the callPop.js try to use an array to add each marker, ie: marcadores.push(new google.maps.Marker({"map": mapa, "position": new google.maps.LatLng(arq.lat, arq.long)}));

  • It worked. It was referral error. Thanks for the help!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.