2012-09-07 5 views
4

나는 출발지에서 목적지 Google지도 API 및 phonegap을 사용하여 현재 위치 + 길 찾기를 표시 하시겠습니까?

  • 표시 사용자의 현재 위치에 다음과 같은 요구 사항

    1. 표시 방향을 가지고있다.

    길 찾기가 포함 된지도를 만들고 그 위에 사용자의 현재 위치를 표시합니다. 사용자가 방향을 도시 작성된 맵 내이지만, 사용자가지도 영역 외에있는 경우, 자신의 현재 위치가 표시되지 않는 경우

    function showDirection(orgn, dstntn) 
    { 
        var directionsService = new google.maps.DirectionsService(); 
        var directionsDisplay = new google.maps.DirectionsRenderer(); 
    
        var map = new google.maps.Map(document.getElementById('displayMap'), { 
         zoom : 7, 
         mapTypeId : google.maps.MapTypeId.ROADMAP 
        }); 
    
        directionsDisplay.setMap(map); 
        directionsDisplay.setPanel(document.getElementById('displayDirections')); 
    
        var request = { 
         origin : orgn, 
         destination : dstntn, 
         travelMode : google.maps.DirectionsTravelMode.WALKING 
        }; 
    
        directionsService.route(request, function(response, status) { 
         if (status == google.maps.DirectionsStatus.OK) { 
          directionsDisplay.setDirections(response); 
         } 
        }); 
        var win = function(position) { 
          var lat = position.coords.latitude; 
          var long = position.coords.longitude; 
          var myLatlng = new google.maps.LatLng(lat, long); 
          var iconimage="images/current_location_small.png"; 
          var marker = new google.maps.Marker({ 
           position: myLatlng, 
           map: map 
           icon: iconimage 
          }); 
    
          marker.setMap(map); 
        }; 
    
        var fail = function(e) { 
          alert('Can\'t retrieve position.\nError: ' + e); 
         }; 
    
        var watchID = navigator.geolocation.getCurrentPosition(win, fail); 
    } 
    

    위의 코드는 잘 작동한다.

    어떻게 든 원점, 2. 목적지 및 3. 사용자 위치가 맵에 적합해야합니다. 세 점 모두에 맞게지도를 확대하거나 세 점 모두를 볼 수있는 방식으로 원본지도를 만드는 방법이 있습니까?

  • 답변

    4

    후 다음을 추가 marker.setMap(map);

    marker.setMap(map); 
    //Add these lines to include all 3 points in the current viewport 
    var bounds = new google.maps.LatLngBounds(); 
    bounds.extend(orgn); 
    bounds.extend(dstntn); 
    bounds.extend(myLatlng); 
    map.fitBounds(bounds); 
    
    관련 문제