2017-04-19 3 views
-1

start = $("#from").val();,の値のオプションをユーザーの現在の位置に変更するにはどうすればよいですか?開始アドレスを入力しなくても、より快適です。Googleマップを使用したjavascriptの指示

助けが必要ですか?

<script type="text/javascript"> 

     $(document).on("pageinit", "#map_page", function() { 
      initialize(); 
     }); 

     $(document).on('click', '#submit', function(e) { 
      e.preventDefault(); 
      calculateRoute(); 
     }); 

     var directionDisplay, 
      directionsService = new google.maps.DirectionsService(), 
      map; 

     function initialize() 
     { 


      directionsDisplay = new google.maps.DirectionsRenderer(); 
      directionsService = new google.maps.DirectionsService(); 
      var mapCenter = new google.maps.LatLng(51.507009, -0.170720); 

      var myOptions = { 
       zoom:16, 
       mapTypeId: google.maps.MapTypeId.ROADMAP, 
       center: mapCenter 
      } 

      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
      directionsDisplay.setMap(map); 
      directionsDisplay.setPanel(document.getElementById("directions")); 
     } 





     function calculateRoute() 
     { 
      var selectedMode = $("#mode").val(), 
       start = $("#from").val();, 
       end = new google.maps.LatLng(51.507009, -0.170720); 
       //start = $("#from").val(); 

      if(start == '' || end == '') 
      { 
       // cannot calculate route 
       $("#results").hide(); 
       return; 
      } 
      else 
      { 
       var request = { 
        origin:start, 
        destination:end, 
        travelMode: google.maps.DirectionsTravelMode[selectedMode] 
       }; 

       directionsService.route(request, function(response, status) { 
        if (status == google.maps.DirectionsStatus.OK) { 
         directionsDisplay.setDirections(response); 
         $("#results").show(); 
         /* 
          var myRoute = response.routes[0].legs[0]; 
          for (var i = 0; i < myRoute.steps.length; i++) { 
           alert(myRoute.steps[i].instructions); 
          } 
         */ 
        } 
        else { 
         $("#results").hide(); 
        } 
       }); 

      } 
     } 
    </script> 
+0

'navigator.geolocation.getCurrentPostition(successFunc)'または 'navigator.geolocation.watchPostition(successFunc)'にHTTP、HTTPSを使用しないことです。 '.lositude'と' .longitude'プロパティを持つ '.coords'を用いた' position'は 'successFunc'に渡されます。 '.getCurrentPosition'が呼び出されるたびにユーザのブラウザが場所を追跡できるかどうか尋ねるので、' .watchPosition'をお勧めします。 'position.coords.latitude'と' position.coords.longitude'に依存する 'successFunc'の中では、すべてが非同期メソッドであるため、すべてを呼び出さなければならないことに注意してください。 https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition – PHPglue

+0

関連する質問:[Google maps directionsServiceを呼び出すときにキャッチされない例外](http://stackoverflow.com/questions/29476953/uncaught) -exception-when-calling-google-maps-directionsservice) – geocodezip

+0

関連の質問:[ユートピアを使用して、ジオロケーションとGoogleマップの指示とユーザー入力を表示しようとしています。実際の方向を除いてすべて動作する](http://stackoverflow.com/questions/35544749/trying-to-display-google-maps-directions-with-geolocation-and-user-input-using) – geocodezip

答えて

0

溶液は、getCurrentPosition() and watchPosition()

関連する問題