0

Estou tentando fazer um simples plugin que mostre via shortcode um pequeno mapa do google maps com a localização atual do usuário, mas quando coloco o shortcode o mapa não aparece, segue o código:

function GetMyLocation(){

?>

<div id="map"></div>
<script>

  function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: -34.397, lng: 150.644},
      zoom: 6
    });
    var infoWindow = new google.maps.InfoWindow({map: map});

    // Try HTML5 geolocation.
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        var pos = {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        };

        infoWindow.setPosition(pos);
        infoWindow.setContent('Location found.');
        map.setCenter(pos);
      }, function() {
        handleLocationError(true, infoWindow, map.getCenter());
      });
    } else {
      // Browser doesn't support Geolocation
      handleLocationError(false, infoWindow, map.getCenter());
    }
  }

  function handleLocationError(browserHasGeolocation, infoWindow, pos) {
    infoWindow.setPosition(pos);
    infoWindow.setContent(browserHasGeolocation ?
                          'Error: The Geolocation service failed.' :
                          'Error: Your browser doesn\'t support geolocation.');
  }
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDn4VzI0_FK0rrbg0QHcLH0l-_n4eFoHnk&callback=initMap">
</script>
3
  • aparece algum erro no console? posta o código do shortcode e como você está inserindo ele no conteúdo também. 8/11/2016 às 12:06
  • Não aparece erro algum no console infelizmente, quando vou inspecionar o elemento a div id="map" recebe style="position: relative; overflow: hidden;" sozinha. Estou inserindo o shortcode [location] dentro de um post @Ricardo 8/11/2016 às 12:16
  • Após fechar o script ainda tem <?php } add_shortcode('location', 'GetMyLocation'); ?> 8/11/2016 às 12:41

0

Você deve fazer log-in para responder a esta pergunta.

Pesquise outras perguntas com a tag .