2013-07-22 3 views
0

У меня проблема с API-интерфейсом google. Я должен добавить на свой сайт карту, отображающую позицию пользователя и маршрут к другому пункту с префиксом.google maps api, указания из 2 баллов

1) Я не могу указать «происхождение» запроса, переменную pos из геолокации.

2) Я не могу дать автомасштабирование с расстоянием coorect для просмотра двух маркеров.

Это мой код:

var directionsService = new google.maps.DirectionsService(); 
var directionsDisplay = new google.maps.DirectionsRenderer(); 

var request = { 
    travelMode: google.maps.DirectionsTravelMode.DRIVING 
    }; 

//Initializing map 
var initialize = function() { 

    // Taking the address of school 
    var address = document.getElementById('address').firstChild.nodeValue; 
    var city = document.getElementById("city").firstChild.nodeValue; 
    var comun = document.getElementById("comun").firstChild.nodeValue; 
    var search = address + " " + city + " " + comun; 

    // Initializing the geocoder and searcing the address in search 
    var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({'address': search}, function(results,status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     var marker2 = new google.maps.Marker({ 
     position: results[0].geometry.location, 
     map: map, 
     title: 'Posizione scuola' 
     }); 
    } else { 
     alert("Problema nella ricerca dell'indirizzo: " + status); 
    } 
    }); 

    // Input the visualization options 
    var options = { 
    zoom: 12, 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    disableDefaultUI: true 
    }; 

    // Create the map 
    var map = new google.maps.Map(document.getElementById('maps'), options); 

    // Try HTML5 geolocation 
    if(navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
     var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
     map.setCenter(pos); 
     request.origin = (map.center); 
     // Put the marker 
     var marker = new google.maps.Marker({ 
     position: pos, 
     map: map, 
     title: 'Tua posizione' 
     }); 
    }); 
    } else { 
    alert("Il tuo dispositivo non permette di visualizzare la tua posizione"); 
    } 

    directionsDisplay.setMap(map); 
    calcRoute(); 
} // End initialize 

var calcRoute =function() { 

    // Taking the address of school 
    var address = document.getElementById('address').firstChild.nodeValue; 
    var city = document.getElementById("city").firstChild.nodeValue; 
    var comun = document.getElementById("comun").firstChild.nodeValue; 
    var search = address + " " + city + " " + comun; 

    request.destination = search; 

    directionsService.route(request, function(response, status) { 
    if (status == google.maps.DirectionsStatus.OK) { 
     directionsDisplay.setDirections(response); 
    } 
    }); 
} 

window.onload = initialize; 

Спасибо за вашу помощь

ответ

0

1.) Вы должны переместить вызов calcRoute в успех обратного вызова из getCurrentPosition (geolocating асинхронный процесс, в настоящее время request.origin еще не установлен, когда вы звоните calcRoute)

if(navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
     var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
     map.setCenter(pos); 
     request.origin = (pos); 
     // Put the marker 
     var marker = new google.maps.Marker({ 
     position: pos, 
     map: map, 
     title: 'Tua posizione' 
     }); 
     directionsDisplay.setMap(map); 
     calcRoute(); 
    }); 
    } else { 
    alert("Il tuo dispositivo non permette di visualizzare la tua posizione"); 
    } 
} 

2.) будет работать, если вы исправили 1.)