2016-05-24 13 views
5

Sto usando questo codice da Google Map API 's e non sta funzionandoQuesta pagina non ha caricato Google Maps correttamente. Vedere la console JavaScript per i dettagli tecnici

<!DOCTYPE html> 
<html> 
<head> 
<title>Place Autocomplete</title> 
<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> 
</head> 
<body> 
<input id="pac-input" class="controls" type="text" 
    placeholder="Enter a location"> 
<div id="type-selector" class="controls"> 
    <input type="radio" name="type" id="changetype-all" checked="checked"> 
    <label for="changetype-all">All</label> 

    <input type="radio" name="type" id="changetype-establishment"> 
    <label for="changetype-establishment">Establishments</label> 

    <input type="radio" name="type" id="changetype-address"> 
    <label for="changetype-address">Addresses</label> 

    <input type="radio" name="type" id="changetype-geocode"> 
    <label for="changetype-geocode">Geocodes</label> 
</div> 
<div id="map"></div> 

<script> 
    // This example requires the Places library. Include the libraries=places 
    // parameter when you first load the API. For example:       // <script src="https://maps.googleapis.com/maps/api/js? key=YOUR_API_KEY&libraries=places"> 

    function initMap() { 
    var map = new google.maps.Map(document.getElementById('map'), { 
     center: {lat: -33.8688, lng: 151.2195}, 
     zoom: 13 
    }); 
    var input = /** @type {!HTMLInputElement} */(
     document.getElementById('pac-input')); 

    var types = document.getElementById('type-selector'); 
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); 
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(types); 

    var autocomplete = new google.maps.places.Autocomplete(input); 
    autocomplete.bindTo('bounds', map); 

    var infowindow = new google.maps.InfoWindow(); 
    var marker = new google.maps.Marker({ 
     map: map, 
     anchorPoint: new google.maps.Point(0, -29) 
    }); 

    autocomplete.addListener('place_changed', function() { 
     infowindow.close(); 
     marker.setVisible(false); 
     var place = autocomplete.getPlace(); 
     if (!place.geometry) { 
     window.alert("Autocomplete's returned place contains no geometry"); 
     return; 
     } 

     // If the place has a geometry, then present it on a map. 
     if (place.geometry.viewport) { 
     map.fitBounds(place.geometry.viewport); 
     } else { 
     map.setCenter(place.geometry.location); 
     map.setZoom(17); // Why 17? Because it looks good. 
     } 
     marker.setIcon(/** @type {google.maps.Icon} */({ 
     url: place.icon, 
     size: new google.maps.Size(71, 71), 
     origin: new google.maps.Point(0, 0), 
     anchor: new google.maps.Point(17, 34), 
     scaledSize: new google.maps.Size(35, 35) 
     })); 
     marker.setPosition(place.geometry.location); 
     marker.setVisible(true); 

     var address = ''; 
     if (place.address_components) { 
     address = [ 
      (place.address_components[0] && place.address_components[0].short_name || ''), 
      (place.address_components[1] && place.address_components[1].short_name || ''), 
      (place.address_components[2] && place.address_components[2].short_name || '') 
     ].join(' '); 
     } 

     infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address); 
     infowindow.open(map, marker); 
    }); 

    // Sets a listener on a radio button to change the filter type on Places 
    // Autocomplete. 
    function setupClickListener(id, types) { 
     var radioButton = document.getElementById(id); 
     radioButton.addEventListener('click', function() { 
     autocomplete.setTypes(types); 
     }); 
    } 

    setupClickListener('changetype-all', []); 
    setupClickListener('changetype-address', ['address']); 
    setupClickListener('changetype-establishment', ['establishment']); 
    setupClickListener('changetype-geocode', ['geocode']); 
    } 
</script> 
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap" 
    async defer></script> 

+1

Cosa javascript console dice? – choz

+0

Possibile duplicato di [ERRORE: errore dell'API di Google Maps: MissingKeyMapError] (http://stackoverflow.com/questions/37991340/error-google-maps-api-error-missingkeymaperror) – user2314737

risposta

23

Google recently changed le condizioni di utilizzo delle sue API di Google Maps; se li stavi già utilizzando su un sito web (diverso da localhost) prima del 22 giugno 2016, non cambierà nulla per te; in caso contrario, riceverai il problema summenzionato e avrai bisogno di una chiave API per correggere il tuo errore. La chiave API gratuita è valida fino a 25.000 carichi di mappe al giorno.

In questo articolo troverete tutto il necessario sapere per quanto riguarda l'argomento, tra cui un tutorial per risolvere il tuo errore:

Google Maps API error: MissingKeyMapError [SOLVED]

Inoltre, ricorda di sostituire YOUR_API_KEY con la vostra chiave API attuale !

+0

Questa era la risposta che stavo cercando! – CodeIt

6

La correzione è davvero semplice: basta sostituire YOUR_API_KEY sull'ultima riga del codice con la tua vera chiave API!

Se non ne possiedi uno, puoi ottenerlo gratuitamente su Google Developers Website.

2

ci 2 possibilità di questo problema:

a. non hai inserito l'API KEY per il browser della mappa

b. non hai consentendo la libreria API espcially per questa API di Google Maps JavaScript

basta controllare la console degli sviluppatori di Google per quel 2 articoli

Problemi correlati