2017-08-10 7 views
-1

私はGoogle Maps APIを使用しています。ページを読み込む際に問題なく動作していますが、ボタンがonclickの場合、Googleマップを表示したいと考えています。私の作業コード: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イベントにしようとしたコード:私はは、AJAX(console.log(res);)での応答を取得しています

$('#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); 
     } 
     }); 

}); 

{ 
"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" 
    } 
] 
} 

私の期待する答え:

私は"status": "success",を取得しているときにAJAXの応答応答で、その時だけ私はマップを表示したい、これを行うには?

+0

が提供してくださいを[MCVE]あなたが問題を示しています尋ねる – geocodezip

答えて

0

マップを最初に初期化しない場合は、URLのコールバック関数を削除します。

&callback=initMap 

それ以外の場合は:APIの準備ができたら

、それは コールバックパラメータを使用して指定された関数を呼び出します。

後、JSONによるstatus成功かどうかをチェックし、ボタンでマップを初期化するために、そして、initMap機能を呼び出す:

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

Mr @ Gurkan Yesilyurt、私はあなたの方法を試してみました。私のjsでは今や緯度と経度は静的ですが、私はそれをダイナミックにしたい、私のjsonレスポンスのように私は2つの結果(lat&lng)私は地図を表示したいだけですが、どうすればいいですか? –

+0

Mr @ Gurkan、こちらをご覧ください。var waypts = []; waypts.push({ 場所:{ LAT:12.9583665、 LNG:77.6635659 } // HAL 経由地:真 }、{ 場所:{ LAT:12.9630167、 LNG:77.6268656 }、 ストップオーバー:true }); ここで私は代わりにstatic lat&lngの値を与えています。これは私の動的lat&lng(JSONレスポンスから)を与えたいです。 –

+0

@subikshanMあなたは必要なデータを取得して関数を渡すことができます。例えば;最初のlatitduteを取得するには、データが配列なのでres.data [0] .latitduteと入力します。そして、この回答を記入し、受け入れられたとマークしてください。おそらくこのことについて新しい質問を開く方がいいでしょう。 –

関連する問題