2017-07-27 67 views
0

我的MVC 5項目中有一個谷歌地圖,其中包含原始位置和目標位置。我需要將原始位置更改爲設置的地址,並且每次我需要使用它時都不會更改,而不必輸入地址。將原始位置更改爲當前位置

它的兩個文本字段與起始位置和目標位置。我希望它的原點位置只能設置爲一個物理地址。

下面是我的代碼:

<!DOCTYPE html> 
 
<html> 
 
<head > 
 
    <title>Place Autocomplete</title> 
 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
 
    <meta charset="utf-8"> 
 
    <style> 
 
     /* Always set the map height explicitly to define the size of the div 
 
     * element that contains the map. */ 
 
     #map { 
 
     height: 10%; 
 
     width:100%; 
 
     padding-top: 800px; 
 
     } 
 
     /* Optional: Makes the sample page fill the window. */ 
 
     html, body { 
 
     height: 10%; 
 
     width:100%; 
 
     
 
    
 
     } 
 
     .controls { 
 
      
 
     margin-top: 10px; 
 
     border: 1px solid transparent; 
 
     border-radius: 2px 0 0 2px; 
 
     box-sizing: border-box; 
 
     -moz-box-sizing: border-box; 
 
     height: 32px; 
 
     outline: none; 
 
     box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3); 
 
     } 
 

 
     #origin-input, 
 
     #destination-input { 
 
     
 
     background-color: #fff; 
 
     font-family: Roboto; 
 
     font-size: 15px; 
 
     font-weight: 300; 
 
     margin-left: 12px; 
 
     padding: 0 11px 0 13px; 
 
     text-overflow: ellipsis; 
 
     width: 200px; 
 
     } 
 

 
     #origin-input:focus, 
 
     #destination-input:focus { 
 
     border-color: #4d90fe; 
 
     } 
 

 
     #mode-selector { 
 
     color: #fff; 
 
     background-color: #4d90fe; 
 
     margin-left: 12px; 
 
     padding: 5px 11px 0px 11px; 
 
     } 
 

 
     #mode-selector label { 
 
     font-family: Roboto; 
 
     font-size: 13px; 
 
     font-weight: 300; 
 
     } 
 

 
    </style> 
 

 
</head> 
 
<body> 
 
    <input id="origin-input" class="controls" type="text" 
 
      value ="8 Parsia Lane, Tongaat, South Africa" > 
 

 
    <input id="destination-input" class="controls" type="text" 
 
      placeholder="Enter a destination location"> 
 

 
    <div id="mode-selector" class="controls"> 
 
     <input type="radio" name="type" id="changemode-walking" checked="checked"> 
 
     <label for="changemode-walking">Walking</label> 
 

 
     <input type="radio" name="type" id="changemode-transit"> 
 
     <label for="changemode-transit">Transit</label> 
 

 
     <input type="radio" name="type" id="changemode-driving"> 
 
     <label for="changemode-driving">Driving</label> 
 
    </div> 
 

 
    <div id="map"></div> 
 

 
    <script> 
 
     // This example requires the Places library. Include the libraries=places 
 
     // parameter when you first load the API. For example: 
 
     
 

 
     function initMap() { 
 
     var map = new google.maps.Map(document.getElementById('map'), { 
 
      mapTypeControl: false, 
 
      center: {lat: -33.8688, lng: 151.2195}, 
 
      zoom: 13 
 
     }); 
 

 
     new AutocompleteDirectionsHandler(map); 
 
     } 
 

 
    // 
 
      
 
     
 
     function AutocompleteDirectionsHandler(map) { 
 
     this.map = map; 
 
     this.originPlaceId = null; 
 
     this.destinationPlaceId = null; 
 
     this.travelMode = 'WALKING'; 
 
     var originInput = document.getElementById('origin-input'); 
 
     var destinationInput = document.getElementById('destination-input'); 
 
     var modeSelector = document.getElementById('mode-selector'); 
 
     this.directionsService = new google.maps.DirectionsService; 
 
     this.directionsDisplay = new google.maps.DirectionsRenderer; 
 
     this.directionsDisplay.setMap(map); 
 

 
     var originAutocomplete = new google.maps.places.Autocomplete(
 
      originInput, {placeIdOnly: true}); 
 
     var destinationAutocomplete = new google.maps.places.Autocomplete(
 
      destinationInput, {placeIdOnly: true}); 
 

 
     this.setupClickListener('changemode-walking', 'WALKING'); 
 
     this.setupClickListener('changemode-transit', 'TRANSIT'); 
 
     this.setupClickListener('changemode-driving', 'DRIVING'); 
 

 
     this.setupPlaceChangedListener(originAutocomplete, 'ORIG'); 
 
     this.setupPlaceChangedListener(destinationAutocomplete, 'DEST'); 
 

 
     this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(originInput); 
 
     this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(destinationInput); 
 
     this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(modeSelector); 
 
     } 
 

 
     // Sets a listener on a radio button to change the filter type on Places 
 
     // Autocomplete. 
 
     AutocompleteDirectionsHandler.prototype.setupClickListener = function(id, mode) { 
 
     var radioButton = document.getElementById(id); 
 
     var me = this; 
 
     radioButton.addEventListener('click', function() { 
 
      me.travelMode = mode; 
 
      me.route(); 
 
     }); 
 
     }; 
 

 
     AutocompleteDirectionsHandler.prototype.setupPlaceChangedListener = function(autocomplete, mode) { 
 
     var me = this; 
 
     autocomplete.bindTo('bounds', this.map); 
 
     autocomplete.addListener('place_changed', function() { 
 
      var place = autocomplete.getPlace(); 
 
      if (!place.place_id) { 
 
      window.alert("Please select an option from the dropdown list."); 
 
      return; 
 
      } 
 
      if (mode === 'ORIG') { 
 
      me.originPlaceId = place.place_id; 
 
      } else { 
 
      me.destinationPlaceId = place.place_id; 
 
      } 
 
      me.route(); 
 
     }); 
 

 
     }; 
 

 
     AutocompleteDirectionsHandler.prototype.route = function() { 
 
     if (!this.originPlaceId || !this.destinationPlaceId) { 
 
      return; 
 
     } 
 
     var me = this; 
 

 
     this.directionsService.route({ 
 
      origin: {'placeId': this.originPlaceId}, 
 
      destination: {'placeId': this.destinationPlaceId}, 
 
      travelMode: this.travelMode 
 
     }, function(response, status) { 
 
      if (status === 'OK') { 
 
      me.directionsDisplay.setDirections(response); 
 
      } else { 
 
      window.alert('Directions request failed due to ' + status); 
 
      } 
 
     }); 
 
     }; 
 

 
    </script> 
 
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAcXA5KHYBhOQPuV3ygleDCPu8w2BHq4OY&libraries=places&callback=initMap" 
 
      async defer></script> 
 
</body> 
 
</html>

+0

不要你原點設置爲一地址? 'value =「8南非Tongaat Parsia Lane」' –

+0

我確實只是選擇了那個值,當我點擊自動完成 –

回答

0

更新您的代碼如下:

<!DOCTYPE html> 
<html> 
<head > 
<title>Place Autocomplete</title> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
<meta charset="utf-8"> 
<style> 
    /* Always set the map height explicitly to define the size of the div 
    * element that contains the map. */ 
    #map { 
    height: 80%; 
    } 
    /* Optional: Makes the sample page fill the window. */ 
    html, body { 
    height: 100%; 
    margin: 0; 
    padding: 0; 
    } 
    /* #map { 
    height: 100%; 
    width:100%; 
    padding-top: 800px; 
    } 

    html, body { 
    height: 10%; 
    width:100%; 
    top: 0; 
    bottom: 0; 
    } */ 
    .controls {   
    margin-top: 10px; 
    border: 1px solid transparent; 
    border-radius: 2px 0 0 2px; 
    box-sizing: border-box; 
    -moz-box-sizing: border-box; 
    height: 32px; 
    outline: none; 
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3); 
    } 

    #origin-input, 
    #destination-input { 

    background-color: #fff; 
    font-family: Roboto; 
    font-size: 15px; 
    font-weight: 300; 
    margin-left: 12px; 
    padding: 0 11px 0 13px; 
    text-overflow: ellipsis; 
    width: 200px; 
    } 

    #origin-input:focus, 
    #destination-input:focus { 
    border-color: #4d90fe; 
    } 

    #mode-selector { 
    color: #fff; 
    background-color: #4d90fe; 
    margin-left: 12px; 
    padding: 5px 11px 0px 11px; 
    } 

    #mode-selector label { 
    font-family: Roboto; 
    font-size: 13px; 
    font-weight: 300; 
    } 

</style> 

</head> 
<body> 
<header> 
    <h2>This is my header</h2> 
</header> 
<input id="origin-input" class="controls" type="text" 
     value ="Sydney, Australia" > 

<input id="destination-input" class="controls" type="text" 
     placeholder="Enter a destination location"> 

<div id="mode-selector" class="controls"> 
    <input type="radio" name="type" id="changemode-walking" checked="checked"> 
    <label for="changemode-walking">Walking</label> 

    <input type="radio" name="type" id="changemode-transit"> 
    <label for="changemode-transit">Transit</label> 

    <input type="radio" name="type" id="changemode-driving"> 
    <label for="changemode-driving">Driving</label> 
</div> 

<div id="map"></div> 

<script> 
    // This example requires the Places library. Include the libraries=places 
    // parameter when you first load the API. For example: 


    function initMap() { 
    var map = new google.maps.Map(document.getElementById('map'), { 
     mapTypeControl: false, 
     center: {lat: -33.8688, lng: 151.2195}, 
     zoom: 13 
    }); 

    new AutocompleteDirectionsHandler(map); 
    } 

// 


    function AutocompleteDirectionsHandler(map) { 
    this.map = map; 
    this.originPlaceId = null; 
    this.destinationPlaceId = null; 
    this.travelMode = 'WALKING'; 
    var originInput = document.getElementById('origin-input'); 
    var destinationInput = document.getElementById('destination-input'); 
    var modeSelector = document.getElementById('mode-selector'); 
    this.directionsService = new google.maps.DirectionsService; 
    this.directionsDisplay = new google.maps.DirectionsRenderer; 
    this.directionsDisplay.setMap(map); 

    // var originAutocomplete = new google.maps.places.Autocomplete(
    //  originInput, {placeIdOnly: true}); 
    var destinationAutocomplete = new google.maps.places.Autocomplete(
     destinationInput, {placeIdOnly: true}); 

    this.setupClickListener('changemode-walking', 'WALKING'); 
    this.setupClickListener('changemode-transit', 'TRANSIT'); 
    this.setupClickListener('changemode-driving', 'DRIVING'); 

    // this.setupPlaceChangedListener(originAutocomplete, 'ORIG'); 
    this.setupPlaceChangedListener(destinationAutocomplete, 'DEST'); 

    this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(originInput); 
    this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(destinationInput); 
    this.map.controls[google.maps.ControlPosition.TOP_LEFT].push(modeSelector); 
    } 

    // Sets a listener on a radio button to change the filter type on Places 
    // Autocomplete. 
    AutocompleteDirectionsHandler.prototype.setupClickListener = function(id, mode) { 
    var radioButton = document.getElementById(id); 
    var me = this; 
    radioButton.addEventListener('click', function() { 
     me.travelMode = mode; 
     me.route(); 
    }); 
    }; 

    AutocompleteDirectionsHandler.prototype.setupPlaceChangedListener = function(autocomplete, mode) { 
    var me = this; 
    autocomplete.bindTo('bounds', this.map); 
    autocomplete.addListener('place_changed', function() { 
     var place = autocomplete.getPlace(); 
     if (!place.place_id) { 
     window.alert("Please select an option from the dropdown list."); 
     return; 
     } 
     if (mode === 'ORIG') { 
     me.originPlaceId = place.place_id; 
     } else { 
     me.destinationPlaceId = place.place_id; 
     } 
     me.route(); 
    }); 

    }; 

    AutocompleteDirectionsHandler.prototype.route = function() { 
    //if (!this.originPlaceId || !this.destinationPlaceId) { 
    if (!this.destinationPlaceId) { 
     return; 
    } 
    var me = this; 

    this.directionsService.route({ 
     origin: document.getElementById('origin-input').value, 
     destination: {'placeId': this.destinationPlaceId}, 
     travelMode: this.travelMode 
    }, function(response, status) { 
     if (status === 'OK') { 
     me.directionsDisplay.setDirections(response); 
     } else { 
     window.alert('Directions request failed due to ' + status); 
     } 
    }); 
    }; 

</script> 
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAcXA5KHYBhOQPuV3ygleDCPu8w2BHq4OY&libraries=places&callback=initMap" 
     async defer></script> 

+0

我試過了,但我仍然需要點擊自動完成才能保存該值我必須輸入目標地址,然後才能正常工作 –

+0

您也可以將自動填充設置爲關閉。您只需要將原點輸入的值固定。所以你必須輸入一個目的地址。或者我錯過了什麼? – eNVy

+0

我發現了這個問題。您的原點輸入由自動完成功能觸發。因此,您只需從原始輸入中獲​​取值並將其設置在directionsService調用中即可。我已經更新了答案。 – eNVy