2017-08-10 50 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,因为数据是数组。并且请将这个答案标记为已接受,也许更好地提出这个问题。 –

相关问题