2017-08-10 37 views
-1

我正在使用Google Maps API。在加載頁面時,它工作正常,但對於我的要求,當onclick按鈕時,我想顯示Google Map。我的工作代碼:Google Maps API無法在onclick事件中工作

function initMap() { 
 
    var directionsDisplay = new google.maps.DirectionsRenderer; 
 
    var directionsService = new google.maps.DirectionsService; 
 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
    zoom: 14, 
 
    center: { 
 
     lat: 12.9577129, 
 
     lng: 77.6764937 
 
    } // Starting Point Marathahalli 
 
    }); 
 
    directionsDisplay.setMap(map); 
 

 
    calculateAndDisplayRoute(directionsService, directionsDisplay); 
 

 
} 
 

 
function calculateAndDisplayRoute(directionsService, directionsDisplay) { 
 
    var selectedMode = document.getElementById('mode').value; 
 

 
    /* configure waypoints */ 
 
    var waypts = []; 
 
    waypts.push({ 
 
    location: { 
 
     lat: 12.9583665, 
 
     lng: 77.6635659 
 
    }, // HAL 
 
    stopover: true 
 
    }, { 
 
    location: { 
 
     lat: 12.9630167, 
 
     lng: 77.6268656 
 
    }, 
 
    stopover: true 
 
    }); 
 

 
    directionsService.route({ 
 
    origin: { 
 
     lat: 12.9577129, 
 
     lng: 77.6764937 
 
    }, // Haight. 
 
    destination: { 
 
     lat: 12.9868068, 
 
     lng: 77.6070679 
 
    }, // Ending Point Shivaji Nagar. 
 
    travelMode: google.maps.TravelMode[selectedMode], 
 
    waypoints: waypts 
 
    }, function(response, status) { 
 
    if (status == 'OK') { 
 
     directionsDisplay.setDirections(response); 
 
     console.log(response); 
 
    } else { 
 
     window.alert('Directions request failed due to ' + status); 
 
    } 
 
    }); 
 
}
/* Always set the map height explicitly to define the size of the div 
 
     * element that contains the map. */ 
 

 
#map { 
 
    height: 100%; 
 
} 
 

 

 
/* Optional: Makes the sample page fill the window. */ 
 

 
html, 
 
body { 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
#floating-panel { 
 
    position: absolute; 
 
    top: 10px; 
 
    left: 25%; 
 
    z-index: 5; 
 
    background-color: #fff; 
 
    padding: 5px; 
 
    border: 1px solid #999; 
 
    text-align: center; 
 
    font-family: 'Roboto', 'sans-serif'; 
 
    line-height: 30px; 
 
    padding-left: 10px; 
 
}
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
 
    <meta charset="utf-8"> 
 
    <title>Travel modes in directions</title> 
 

 
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC7lDrYPDmJz1JsQh2rbWA9uRZHcFk_xJY&callback=initMap"> 
 
    </script> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
</head> 
 

 
<body> 
 
    <form id="testForm"> 
 

 
    <select name="tripId"> 
 
\t \t <option value="">Select trip</option> 
 
\t \t <option value="1">1</option> 
 
\t \t <option value="2">2</option> 
 
\t </select> 
 

 
    <button type="button" id="btn-submit">submit</button> 
 
    </form><br></br> 
 

 
    <div id="floating-panel"> 
 
    <b>Mode of Travel: </b> 
 
    <select id="mode"> 
 
     <option value="DRIVING">Driving</option> 
 
    </select> 
 
    </div> 
 
    <div id="map"></div> 
 
</body> 
 

 
</html>

,我與onclick事件試圖代碼:

$('#btn-submit').click(function(e){ 
    e.preventDefault(); 
    $.ajax({ 
     type:'POST', 
     url :"getLatLan.php", 
     data : $('#testForm').serialize(), 
      success: function(data) { 
       var res = jQuery.parseJSON(data); 
      console.log(res); 
      }, 
     error:function(exception){ 
     alert('Exeption:'+exception); 
     } 
     }); 

}); 

上午獲得在AJAX(console.log(res);)的響應:

{ 
"status": "success", 
"count": 2, 
"data": [ 
    { 
     "tripId": "1", 
     "pickUpLatitidute": "", 
     "pickUpLongitude": "", 
     "cabNo": "Ad2K2001", 
     "driverId": "G2E100", 
     "routeId": "1", 
     "tripDate": "2017-08-01 15:45:55", 
     "startTime": "15:45:55", 
     "endTime": "14:48:55", 
     "shiftId": "1", 
     "tripStatus": "0", 
     "id": "1", 
     "empId": "G2E201", 
     "callStartTime": "", 
     "callEndTime": "", 
     "cabReachingTime": "", 
     "pickupTime": "15:50:02", 
     "dropTime": "", 
     "dropStatus": "0", 
     "otp": "421283", 
     "pickupotpStatus": "1", 
     "empPresentStatus": "0", 
     "latitdute": "12.9583665", 
     "longitude": "77.6635659" 
    }, 
    { 
     "tripId": "1", 
     "pickUpLatitidute": "", 
     "pickUpLongitude": "", 
     "cabNo": "Ad2K2001", 
     "driverId": "G2E100", 
     "routeId": "1", 
     "tripDate": "2017-08-01 15:45:55", 
     "startTime": "15:45:55", 
     "endTime": "14:48:55", 
     "shiftId": "1", 
     "tripStatus": "0", 
     "id": "2", 
     "empId": "G2E200", 
     "callStartTime": "", 
     "callEndTime": "", 
     "cabReachingTime": "", 
     "pickupTime": "", 
     "dropTime": "", 
     "dropStatus": "0", 
     "otp": "", 
     "pickupotpStatus": "0", 
     "empPresentStatus": "0", 
     "latitdute": "12.9630167", 
     "longitude": "77.6268656" 
    } 
] 
} 

我的預期答案:

在AJAX響應響應當我得到"status": "success",,那時候我只想顯示我的地圖,我該怎麼做?

+0

請提供[MCVE]演示你的問題詢問。 – geocodezip

回答

0

如果您不想在開始時初始化映射,請移除URL上的回調函數。

&callback=initMap 

否則:

當API準備好,它會調用該函數指定使用 回調參數。

後,檢查status是否成功與否根據JSON,並初始化通過按鈕地圖的話,調用initMap功能:

success: function(data) { 
    var res = jQuery.parseJSON(data); 
    if(res.status == "success") 
    { 
     initMap(); 
    } 
} 
+0

Mr @ Gurkan Yesilyurt,我試過你的方式它工作,在我的js現在經度和緯度都是靜態的,但我想使它動態,就像我的json響應我有2個結果(lat&lng) lng只有我想要顯示地圖,怎麼做到這一點? –

+0

Mr @ Gurkan,請看這裏var waypts = []; waypts.push({ 位置:{ 緯度:12.9583665, LNG:77.6635659 },// HAL 停留:真 },{ 位置:{ 緯度:12.9630167, LNG:77.6268656 }, 中途停留:真正的 }); 在這裏,我給靜態拉特&lng值,而不是我想給我的動態拉特&lng(來自JSON響應) –

+0

@subikshanM你可以得到任何你想要的數據,並傳遞它的功能。例如;要獲得第一個latitdute,請鍵入res.data [0] .latitdute,因爲數據是數組。並且請將這個答案標記爲已接受,也許更好地提出這個問題。 –

相關問題