Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

matheushirota

Duvida Google Maps

Recommended Posts

Olá Galera tudo bem?

Tenho uma duvida, estou com um projeto aonde devo implementar o seguinte:

Um campo para a pessoa digitar o endereço ou o CEP desejado e o site mostrar no google maps personalizado qual a unidade mais próxima daquele endereço ou CEP digitado!

Qual seria a direção para começar a fazer isso?

Compartilhar este post


Link para o post
Compartilhar em outros sites

Eu consegui com esse código:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
.controls {
  margin-top: 10px;
  border: 1px solid transparent;
  border-radius: 2px 0 0 2px;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  height: 32px;
  outline: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

#pac-input {
  background-color: #fff;
  font-family: Roboto;
  font-size: 15px;
  font-weight: 300;
  margin-left: 12px;
  padding: 0 11px 0 13px;
  text-overflow: ellipsis;
  width: 300px;
}

#pac-input:focus {
  border-color: #4d90fe;
}

.pac-container {
  font-family: Roboto;
}

#type-selector {
  color: #fff;
  background-color: #4d90fe;
  padding: 5px 11px 0px 11px;
}

#type-selector label {
  font-family: Roboto;
  font-size: 13px;
  font-weight: 300;
}

    </style>
    <title>Places Searchbox</title>
    <style>
      #target {
        width: 345px;
      }
    </style>
  </head>
  <body>
    <input id="pac-input" class="controls" type="text" placeholder="Search Box">
    <div id="map"></div>
    <script>
function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 10,
    center: {lat: -22.883498, lng: -43.563301}
      
  });
    var input = document.getElementById('pac-input');
  var searchBox = new google.maps.places.SearchBox(input);
  map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
  map.addListener('bounds_changed', function() {
    searchBox.setBounds(map.getBounds());
  });
    var markers = [
        ['Bondi Beach 2', -22.883498, -43.563301, 6],
        ['Bondi Beach', -22.964323, -43.355555, 4],
        ['Coogee Beach', -22.466058, -44.452144, 5],
        ['Cronulla Beach', -22.841209, -43.369073, 3],
        ['Manly Beach', -22.821338, -43.045970, 2],
        ['Maroubra Beach', -22.972315, -44.297408, 1]
    ];
  searchBox.addListener('places_changed', function() {
    var places = searchBox.getPlaces();

    if (places.length == 0) {
      return;
    }
    var bounds = new google.maps.LatLngBounds();
    places.forEach(function(place) {
      var icon = {
        url: 'favicon.png',
        size: new google.maps.Size(70, 70),
        origin: new google.maps.Point(0, 0),
        anchor: new google.maps.Point(70, 70),
        scaledSize: new google.maps.Size(70, 70)
      };
      markers.push(new google.maps.Marker({
        map: map,
        icon: icon,
        title: place.name,
        position: place.geometry.location
      }));
      if (place.geometry.viewport) {
        bounds.union(place.geometry.viewport);
      } else {
        bounds.extend(place.geometry.location);
      }
    });
    map.fitBounds(bounds);
  });
  setMarkers(map);
}
function setMarkers(map) {
    var beaches = [
        ['Bondi Beach 2', -22.883498, -43.563301, 6],
        ['Bondi Beach', -22.964323, -43.355555, 4],
        ['Coogee Beach', -22.466058, -44.452144, 5],
        ['Cronulla Beach', -22.841209, -43.369073, 3],
        ['Manly Beach', -22.821338, -43.045970, 2],
        ['Maroubra Beach', -22.972315, -44.297408, 1]
    ];
    var image = {
        url: 'favicon.png',
        size: new google.maps.Size(70, 70),
        origin: new google.maps.Point(0, 0),
        anchor: new google.maps.Point(0, 70)
    };
    var shape = {
        coords: [1, 1, 1, 20, 18, 20, 18, 1],
        type: 'poly'
    };
    for (var i = 0; i < beaches.length; i++) {
        var beach = beaches[i];
        var marker = new google.maps.Marker({
            position: {lat: beach[1], lng: beach[2]},
            map: map,
            icon: image,
            shape: shape,
            title: beach[0],
            zIndex: beach[3]
        });
    }
}
    </script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?key=&libraries=places&callback=initMap"
         async defer></script>
    <script src="maps.js"></script>
  </body>
</html>

Agora só preciso que ao clicar em cima do marcador ele abra um balão com algumas informações!

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.