Visualizzazione dei risultati da 1 a 2 su 2

Hybrid View

  1. #1

    Ricavare coordinate da indirizzo

    Ciao a tutti, sto provando a studiare e a cercare di realizzare tramite le api di google maps, un javascript che mi permetti di individuare le coordinate di un punto o di una strada.

    Se non ho capito male per realizzare in una mappa varie marker che individuano dei punti devo mettere per ognuno di questi delle coordinate ben precise

    Vorrei creare una pagina però dove l'utente non scriva direttamente le coordinate che giustamente non sa, ma che o tramite geo individuazione intercetto le suo coordinate, o se questo vuole indicare un altra strada, indica il nome di questa e io ottengo quindi le coordinate

    ho trovato un tutorial con esempio,
    http://www.aracnopolis.it/2013/08/google-maps-api-2/

    che mi permette di individuare il mio indirizzo
    ma le coordinate mi compaiono solo se clicco sul marker, mentre io le vorrei già importare in un input text, ma per questo sto facendo delle prove a cercare di modificare il tutorial
    Quello che sicuro non ottengo, e che se vado a digitare un nuovo indirizzo tipo "corso italia 158, milano" , si ricrea la mappa, ma non ottengo il marker con le nuove coordinate.
    Mi sapereste indicare qualche tutorial o se avete uno script già fatto per ottenere le coordinate in cui mi trovo o scrivendo un nuovo indirizzo di ottenere le coordinate di quest'ultimo?

    Grazie in anticipo

  2. #2
    ho inserito due righe di codice, a questo tutorial e forse ho già risolto
    anche se qualche volta non mi funziona, ma non ho capito il problema nello script di base dov'è

    vi posto lo script, avete qualche suggerimento, o comunque qualche tutorial da farmi leggere (possibilmente in italiano)

    <style type="text/css">
    html { height: 100% }
    body { height: 100%; margin: 20px; padding: 0 }
    #map-canvas { height: 80% }
    #footer { margin-top: 20px; text-align: center }
    </style>
    <script type="text/javascript">


    var map;
    var infowindow;
    var userMarker;
    var geocoder;


    function drawMap(p_lat, p_long, p_zoom) {
    var mapOptions = {
    center: new google.maps.LatLng(p_lat,p_long),
    zoom: p_zoom,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
    geocoder = geocoder = new google.maps.Geocoder();
    google.maps.event.addListener(map, 'click', function(event) {
    if ( ! document.getElementById("gobutton").disabled ) {
    if ( infowindow ) infowindow.close();
    infowindow = null;
    userMarker.setClickable(false);
    userMarker.setPosition(event.latLng);
    geocode();
    }


    });
    }


    function geolocate() {
    if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosit ion,showWorld);
    }
    else{
    document.getElementById("map-canvas").innerHTML="Geolocation is not supported by this browser.";
    }
    }


    function goforGeocoding(enable) {
    document.getElementById("gobutton").disabled = !enable;
    }


    function showPosition(location) { //geolocalizzazione riuscita
    drawMap(location.coords.latitude, location.coords.longitude, 10);
    userMarker = new google.maps.Marker({
    position: new google.maps.LatLng(location.coords.latitude, location.coords.longitude),
    map: map,
    title:"Voi siete qui!"
    });
    var contentString = "<b>Salve!</b><br/>La tua posizione è:<br/>" +
    'Latitudine : ' + location.coords.latitude + "<br/>" +
    'Longitudine: ' + location.coords.longitude + "<br/>" +
    'Con un errore di metri: ' + location.coords.accuracy + "<br/>" +
    'Altitudine : ' + location.coords.altitude + "<br/>" +
    'Con un errore di metri: ' + location.coords.altitudeAccuracy + "<br/>";
    document.getElementById("coordinate").value=locati on.coords.latitude + "," +location.coords.longitude
    infowindow = new google.maps.InfoWindow({
    content: contentString
    });


    google.maps.event.addListener(userMarker, 'click', function() {
    infowindow.open(map,userMarker);
    });


    geocode();

    }


    function geocode() {
    goforGeocoding(false);
    geocoder.geocode({
    latLng: userMarker.getPosition(),
    bounds: map.getBounds()
    }, showAddress);

    }


    function resolve() {
    if ( document.getElementById("address").value.length == 0 ) {
    alert("Digitare un indirizzo");
    return;
    }
    goforGeocoding(false);
    geocoder.geocode({
    address: document.getElementById("address").value,
    bounds: map.getBounds()
    }, showMarker);




    }


    function showMarker(results,status) {
    goforGeocoding(true);
    if ( status == google.maps.GeocoderStatus.OK ) {
    map.setCenter(results[0].geometry.location);
    map.setZoom(10);
    document.getElementById("coordinate").value=result s[0].geometry.location
    if( ! userMarker ) {
    userMarker = new google.maps.Marker({
    map: map,
    position: results[0].geometry.location
    });
    infowindow = new google.maps.InfoWindow({
    content: "Coordinate: " + results[0].geometry.location
    });

    google.maps.event.addListener(userMarker, 'click', function() {
    infowindow.open(map,userMarker);
    });
    } else {
    infowindow.setContent("Coordinate: " + results[0].geometry.location);
    userMarker.setPosition(results[0].geometry.location);
    }
    } else if (result == google.maps.GeocoderStatus.UNKNOWN_ERROR) {
    alert("Errore nella richiesta a Google, si consiglia di ricaricare la pagina");


    } else if (result == google.maps.GeocoderStatus.ZERO_RESULTS) {
    alert("Nessun risultato trovato.");
    }
    else {
    alert("Errore: " + status);
    }
    }


    function showAddress(results, status) {
    goforGeocoding(true);
    if ( status == google.maps.GeocoderStatus.OK ) {
    document.getElementById("address").value = results[0].formatted_address;
    } else if (result == google.maps.GeocoderStatus.UNKNOWN_ERROR) {
    alert("Errore nella richiesta a Google, si consiglia di ricaricare la pagina");


    } else if (result == google.maps.GeocoderStatus.ZERO_RESULTS) {
    alert("Nessun risultato trovato.");
    }
    else {
    alert("Errore: " + status);
    }
    }




    function showWorld(error) {
    drawMap(0, 0, 1); //tutto il mondo
    }


    function loadMapAPI() {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.googleapis.com/maps/api/js?key=AIzaSyCLX5OcXGCdPcCdztXXXXXx-lFOoNXT9I&sensor=true&callback=geolocate";
    setTimeout(function () {
    try{
    if (!google || !google.maps) {
    //se lo script di google non è stato caricato
    //caricare la foto di un rage comic o di un gattino
    document.getElementById("map-canvas").innerHTML("Questo non era previsto");
    }
    }
    catch (e) {
    //qualcosa come alert('Qualcosa è andato storto!');
    document.getElementById("map-canvas").innerHTML("Era praticamente impossibile, fino a quando non è successo");
    }
    }, 7000);
    document.body.appendChild(script);
    }
    </script>

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.