私はJavascriptアプリを開発しています。それはやや複雑なアプリケーションの私の最初の試みであり、私は約束の問題にぶち当たっています。Javascript約束しない約束
<!DOCTYPE html>
<html>
<head>
<title>Images</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
...
</style>
<script src="http://js.pusher.com/2.2/pusher.min.js"></script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=key&callback=initMap">
</script>
</head>
<body>
<div id="map"></div>
<script>
var map;
//setup Pusher API
var channel = "channel";
var pusher = new Pusher(appid);
var title;
var Channel = pusher.subscribe(channel);
function getTitle() {
return new Promise(function(resolve, reject) {
Channel.bind("new-listing", function(listing) {
title = listing['title'];
resolve(title);
}, 0);
});
}
getTitle().then(function(title) {
return title;
});
// console.log("Title: " + title);
function findCoordinates() {
return new Promise(function(resolve, reject) {
geocoder.geocode({ 'address': address}, function(results, status) {
latitude = results[0].geometry.location.lat();
longitude = results[0].geometry.location.lng();
resolve(latitude, longitude);
}, 0);
});
}
findCoordinates().then(function(latitude, longitude) {
console.log("Latitude: " + latitude);
console.log("Longitude: " + longitude);
});
// console.log("Latitude: " + latitude);
function initMap() {
var postion = {lat: latitude, lng: longitude}; //here I need latitude and longitude
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: postion
});
var marker = new google.maps.Marker({
position: postion,
map: map,
title: title //here I need title
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
}
</script>
</body>
</html>
私は3つの問題があります。約束は作業しているが、問題は、私は両方の緯度を持っている必要がありますタイトルは緯度+と同じその約束
- をと経度が利用可能
- もう1つの問題は、私のコードが、ジオコーダーが約束の中で定義されていないと不平を言うことです。
フィードバックは本当にありがたいです。
title' 'の範囲が予想されるとして、1(あります'latitude'と' longitude'に反対します)、実際の問題はコールバックの非同期です。 – Bergi
私は同様の質問の答えを読んで、より具体的に私の質問に適応しました。答えを読んだ後でさえ、私はそれを見ないと認めなければなりません。 – wiwa1978
'setTimeout'は' Channel.bind'に似ていて、 'Test message''は' listing ['title'] 'と似ています。その約束と座標の約束をもって、 'Promise.all'を使い、すべてのデータが到着したら' initMap'を呼び出すことができます。 – Bergi