2013-07-01 170 views
1

我正在使用谷歌地圖查找兩個位置之間的路線。我想要獲取路線中心的經緯度。你可以告訴我如何獲得路線的中心。我使用下面的代碼獲取路線,在此先感謝 var map; var directionsDisplay; var directionsService; var stepDisplay; var markerArray = []; var infoWindow; var service; var lat1 = 0; var lng1;如何獲取兩個位置之間的路線中心緯度和經度

function pre_initialize() { 
     var mapOptions = { 
      zoom: 6, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(function (position) { 
       var pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); 
       var infowindow = new google.maps.InfoWindow({ 
        map: map, 
        position: pos, 
        content: 'Current Location.' 
       }); 
       map.setCenter(pos); 
      }, function() { 
       handleNoGeolocation(true); 
      }); 
     } else { 
      // Browser doesn't support Geolocation 
      handleNoGeolocation(false); 
     } 
    } 
    function handleNoGeolocation(errorFlag) { 
     if (errorFlag) { 
      var content = 'Error: The Geolocation service failed.'; 
     } else { 
      var content = 'Error: Your browser doesn\'t support geolocation.'; 
     } 

     var options = { 
      map: map, 
      position: new google.maps.LatLng(60, 105), 
      content: content 
     }; 

     var infowindow = new google.maps.InfoWindow(options); 
     map.setCenter(options.position); 
    } 
    function initialize() { 
     var mapOptions = { 
      zoom: 6, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);      
     var addressfrom = document.getElementById("from").value; 
     var addressto = document.getElementById("to").value; 
     var geocoder = new google.maps.Geocoder(); 
     var coords = new google.maps.LatLng(0, 0); 
     alert(lat1); 
     coords = geocoder.geocode({ 'address': addressfrom }, function (results, status) {    
      results[0].geometry.location.lat(); 
      results[0].geometry.location.lng(); 
     }); 

     directionsService = new google.maps.DirectionsService(); 
     // Create a renderer for directions and bind it to the map. 
     var rendererOptions = { 
      map: map 
     } 
     directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions) 

     // Instantiate an info window to hold step text. 
     infoWindow = new google.maps.InfoWindow(); 
     stepDisplay = new google.maps.InfoWindow(); 
     calcRoute(); 
     google.maps.event.addListenerOnce(map, 'bounds_changed', performSearch); 
    } 



    function performSearch() {    
     var request = { 
      bounds: map.getBounds(), 
      radius: 100, 
      types: ['hospital', 'cafe', 'restaurant', 'food', 'bar'], 
      keyword: 'best view' 
     }; 
     service = new google.maps.places.PlacesService(map); 
     //service.nearbySearch(request, callback);  
     service.radarSearch(request, callback); 
     //service.textSearch(request, callback); 
    } 
    function callback(results, status) {   
     if (status != google.maps.places.PlacesServiceStatus.OK) { 
      alert(status); 
      return; 
     } 
     for (var i = 0, result; result = results[i]; i++) { 
       createMarker(result); 
     } 
    }  
    function createMarker(place) { 
     var marker = new google.maps.Marker({ 
      map: map,     
      position: place.geometry.location, 
      icon: 
      { 
       // Star 
       path: 'M 0,-24 6,-7 24,-7 10,4 15,21 0,11 -15,21 -10,4 -24,-7 -6,-7 z', 
       fillColor: '#ff0000', 
       fillOpacity: 1, 
       scale: 1/4, 
       strokeColor: '#bd8d2c', 
       strokeWeight: 1 
      } 
     }); 

     google.maps.event.addListener(marker, 'click', function() { 

      service.getDetails(place, function (result, status) { 
       if (status != google.maps.places.PlacesServiceStatus.OK) { 
        alert(status); 
        return; 
       } 

       infoWindow.setContent(result.name); 
       infoWindow.open(map, marker); 
      }); 
     }); 
    } 

    function calcRoute() { 

     // First, remove any existing markers from the map. 
     for (var i = 0; i < markerArray.length; i++) { 
      markerArray[i].setMap(null); 
     } 

     // Now, clear the array itself. 
     markerArray = []; 
     var start = document.getElementById('from').value; 
     var end = document.getElementById('to').value; 
     var request = { 
      origin: start, 
      destination: end, 
      travelMode: google.maps.TravelMode.WALKING 
     }; 
     directionsService.route(request, function (response, status) { 
      if (status == google.maps.DirectionsStatus.OK) { 
       var warnings = document.getElementById('warnings_panel'); 
       warnings.innerHTML = '<b>' + response.routes[0].warnings + '</b>'; 
       directionsDisplay.setDirections(response); 
       showSteps(response); 
      } 
     }); 
    } 

    function showSteps(directionResult) {   
     var myRoute = directionResult.routes[0].legs[0]; 

     for (var i = 0; i < myRoute.steps.length; i++) { 
      var marker = new google.maps.Marker({ 
       position: myRoute.steps[i].start_point, 
       map: map 
      }); 
      attachInstructionText(marker, myRoute.steps[i].instructions); 
      markerArray[i] = marker; 
     } 
    } 

    function attachInstructionText(marker, text) { 
     google.maps.event.addListener(marker, 'click', function() {    
      stepDisplay.setContent(text); 
      stepDisplay.open(map, marker); 
     }); 
    } 



</script> 
+1

檢查這個http://stackoverflow.com/questions/10634199/find-center-of-multiple-locations-in-google-maps –

回答

0

請參閱下面的代碼:

self.adjustPosition = function() {  
    var lat = 0, lng = 0; 

    if (self.nearbyPlaces().length == 0) { 
     return false; 
    } 

    for (var i = 0; i < self.nearbyPlaces().length; i++) { 
     lat += self.nearbyPlaces()[i].latitude; 
     lng += self.nearbyPlaces()[i].longitude; 
    } 

    lat = lat/self.nearbyPlaces().length; 
    lng = lng/self.nearbyPlaces().length; 

    self.map.setCenter(new window.google.maps.LatLng(lat, lng)); 
}; 
+0

這代碼將產生不正確的結果,經度爲180E/W – Alnitak

相關問題