Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2007
    residenza
    Trieste
    Messaggi
    831

    geocoding su mappa sbagliato di qualche km

    Ho un problema di geolocalizzazione sulla mappa del sito, perchè all'apertura della mappa il marker viene posizionato a circa 1-2 km di distanza dalla posizione in cui mi trovo. Ho avuto anche la possibilità di testare in un'altra città, ed il problema persiste.
    Vi chiedo cosa ci può essere di sbagliato in questo codice (dove c'è anche la sezione per le direzioni). Grazie in anticipo.

    codice:
    var directionDisplay, map;
    var directionsService = new google.maps.DirectionsService();
    var geocoder = new google.maps.Geocoder();
    
    function initialize() {
        // Create an array of styles.
        var styles = [ { "stylers": [ { "saturation": -71 }, { "lightness": 21 }, { "gamma": 0.62 }, { "hue": "#0091ff" } ] } ];
        // Create a new StyledMapType object, passing it the array of styles, as well as the name to be displayed on the map type control.
        var styledMap = new google.maps.StyledMapType(styles,
          {name: "Styled Map"});
    
        // set the default center of the map
        var latlng = new google.maps.LatLng(41.9000, 12.5000); // Rome
        // set route options (draggable means you can alter/drag the route in the map)
        var rendererOptions = {    draggable: true    };
        directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
        // create a map object (i.e. set the display options for the map) and include the MapTypeId to add to the map type control
        var myOptions = {
            zoom: 13,
            center: new google.maps.LatLng(41.9000, 12.5000),
            disableDefaultUI: true, // controls disables
            mapTypeControlOptions: {mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']}
        };
        
        // add the map to the map placeholder
        map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
        //Associate the styled map with the MapTypeId and set it to display.
          map.mapTypes.set('map_style', styledMap);
          map.setMapTypeId('map_style');
    
        // bind the map to the directions
        directionsDisplay.setMap(map);
        // point the directions to the container for the direction details
        directionsDisplay.setPanel(document.getElementById("directionsPanel"));
        // start the geolocation API
        if (navigator.geolocation) {
            // when geolocation is available on your device, run this function
            navigator.geolocation.getCurrentPosition(foundYou, notFound);
        } else {
            // when no geolocation is available, alert this message
            alert('Geolocation not supported or not enabled.');
        }
    }
    
    // executed when the location of the visitor could not be determined
    function notFound(msg) {  
      alert('I could not find your location :(')
    }
    
    // executed when the location of the visitor can be determined
    function foundYou(position) {
        // convert the position returned by the geolocation API to a google coordinate object
        var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        // then try to reverse geocode the location to return a human-readable address
        geocoder.geocode({'latLng': latlng}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                // if the geolocation was recognized and an address was found
                if (results[0]) {
                    // add a marker to the map on the geolocated point
                    marker = new google.maps.Marker({
                            position: latlng,
                            map: map
                    });
                    // compose a string with the address parts
                    var address = results[0].address_components[1].long_name+' '+results[0].address_components[0].long_name+', '+results[0].address_components[3].long_name
                    // set the located address to the link, show the link and add a click event handler
                    $('.autoLink span').html(address).parent().show().click(function(){
                        // onclick, set the geocoded address to the start-point formfield
                        $('#routeStart').val(address);
                        // call the calcRoute function to start calculating the route
                        calcRoute();
                    });
                }
            } else {
                // if the address couldn't be determined, alert and error with the status message
                alert("Geocoder failed due to: " + status);
            }
        });
    }
    
    // the directions function
    function calcRoute() {
        // get the travelmode, startpoint and via point from the form        
        var travelMode = $('input[name="travelMode"]:checked').val();
        var start = $("#routeStart").val();
        var end = $("#routeEnd").val();
        // compose a array with options for the directions/route request
        var request = {
            origin: start,
            destination: end,
            unitSystem: google.maps.UnitSystem.IMPERIAL,
            travelMode: google.maps.DirectionsTravelMode[travelMode]
        };
        // call the directions API
        directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                // directions returned by the API, clear the directions panel before adding new directions
                $('#directionsPanel').empty();
                // display the direction details in the container
                directionsDisplay.setDirections(response);
            } else {
                // alert an error message when the route could not be calculated.
                if (status == 'ZERO_RESULTS') {
                    alert('No route could be found between the origin and destination.');
                } else if (status == 'UNKNOWN_ERROR') {
                    alert('A directions request could not be processed due to a server error. The request may succeed if you try again.');
                } else if (status == 'REQUEST_DENIED') {
                    alert('This webpage is not allowed to use the directions service.');
                } else if (status == 'OVER_QUERY_LIMIT') {
                    alert('The webpage has gone over the requests limit in too short a period of time.');
                } else if (status == 'NOT_FOUND') {
                    alert('At least one of the origin, destination, or waypoints could not be geocoded.');
                } else if (status == 'INVALID_REQUEST') {
                    alert('The DirectionsRequest provided was invalid.');                    
                } else {
                    alert("There was an unknown error in your request. Requeststatus: \n\n"+status);
                }
            }
        });
    }

  2. #2
    Utente di HTML.it
    Registrato dal
    Oct 2007
    residenza
    Trieste
    Messaggi
    831
    ....nessuna idea su cosa devo correggere? Il marker è geolocalizzato sempre nello stesso punto, ma è sbagliato.

  3. #3
    Moderatore di JavaScript L'avatar di br1
    Registrato dal
    Jul 1999
    Messaggi
    19,998
    Non e' che ci si possa dedicare tanto tempo: occorrerebbe come minimo crearsi una pagina di prova (che magari avrebbe molto di diverso dalla tua reale)

    Posta un link alla tua pagina affinche' la si possa testare.
    Il guaio per i poveri computers e' che sono gli uomini a comandarli.

    Attenzione ai titoli delle discussioni: (ri)leggete il regolamento
    Consultate la discussione in rilievo: script / discussioni utili
    Usate la funzione di Ricerca del Forum

  4. #4
    Utente di HTML.it
    Registrato dal
    Oct 2007
    residenza
    Trieste
    Messaggi
    831
    ok, sono poco esperto in mappe e pensavo che postare il codice fosse sufficiente.
    La pagina live risiede invece su www.studiocasa85.com, cliccando sul lato destro (roooms).

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 © 2025 vBulletin Solutions, Inc. All rights reserved.