2011-08-21 21 views
13

Come posso inserire un set di coordinate e visualizzare le mappe di Google in una posizione. Questo è tutto per essere fatto attraverso javascript (es: senza interfaccia utente)Luogo di visualizzazione Utilizzo di coordinate longitudine/latitudine - Google Maps

Ogni aiuto è apprezzato

+0

ho chiesto lo stesso Ed essi risposero -> http://stackoverflow.com/questions/ 8766116/how-can-i-get-xy-number-at-every-click-on-google-maps – SamekaTV

+0

@SamekaTV - Quel collegamento è l'obiettivo OPPOSTA (facendo clic su una mappa e tornando longitudine/latitudine). QUESTA domanda viene data una longitudine/latitudine nota, come visualizzare la mappa di quella posizione. (O forse shahmeer stava facendo la domanda correlata, su come mostrare un marker in quella posizione, su una mappa che è già aperta.) – ToolmakerSteve

+0

Un blog dettagliato: http://sforsuresh.in/display-google-map-locations- using-latitude-longitude/ –

risposta

11

Nota: Se hai già una latitudine/longitudine, vedi Alteveer’s answer.

Avrete bisogno di utilizzare un servizio di geolocalizzazione per ottenere la latitudine/longitudine. Google Maps ha un costruito in: http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding

Ecco un esempio di come usarlo:

<!-- Placeholder for the Google Map --> 
<div id="map" style="height: 512px;"> 
    <noscript> 
    <!-- http://code.google.com/apis/maps/documentation/staticmaps/ --> 
    <img src="http://maps.google.com/maps/api/staticmap?center=1%20infinite%20loop%20cupertino%20ca%2095014&amp;zoom=16&amp;size=512x512&amp;maptype=roadmap&amp;sensor=false" /> 
    </noscript> 
</div> 

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 

// Define the address we want to map. 
var address = "1 infinite loop cupertino ca 95014"; 

// Create a new Geocoder 
var geocoder = new google.maps.Geocoder(); 

// Locate the address using the Geocoder. 
geocoder.geocode({ "address": address }, function(results, status) { 

    // If the Geocoding was successful 
    if (status == google.maps.GeocoderStatus.OK) { 

    // Create a Google Map at the latitude/longitude returned by the Geocoder. 
    var myOptions = { 
     zoom: 16, 
     center: results[0].geometry.location, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map"), myOptions); 

    // Add a marker at the address. 
    var marker = new google.maps.Marker({ 
     map: map, 
     position: results[0].geometry.location 
    }); 

    } else { 
    try { 
     console.error("Geocode was not successful for the following reason: " + status); 
    } catch(e) {} 
    } 
}); 
</script> 
+1

Questo risponde a domanda diversa da quella richiesta La domanda è: dato LATITUDE/LONGITUDE, mostra una mappa o un marker su una mappa. Questa risposta è: dato un INDIRIZZO, mostra un marcatore su una mappa. ** Se hai già lat/long **, allora vedi le linee sotto '// Aggiungi un marker all'indirizzo 'per visualizzare un marker. O se vuoi aprire una nuova mappa, vedi [risposta di Alteveer] (https://stackoverflow.com/a/7135770/199364). – ToolmakerSteve

+0

Questo è un grande punto, Steve. (Oops!) Ho aggiunto una nota a questa risposta. – Jim

Problemi correlati