関数を実行する方法を理解するのに問題があります-次に、ユーザー入力を待ってから(つまり、ユーザーが場所を共有することを選択した場合)、別の関数を実行します。「getUserLocation」関数が実行されると、ユーザーがアラートウィンドウに入力する前に、「addLocationArrayToMap」関数が実行されます。
一部のデバッグでは「userPosが定義されていません」と返されます。これにより、なぜ機能しないのかについての私の推測は正確だと思います。
以下のコード:
var markers =[];
var myLocationIconArray = [];
var infoWindowContentString = '';
var addInfowindow;
var distanceArray = [];
var addInfowindow;
function runAll() {
getUserLocation(addLocationArrayToMap);
}
function getUserLocation() {
map.setOptions({draggable: true, zoomControl: true, scrollwheel: true, disableDoubleClickZoom: false});
// Another function that deletes 'dummy' markers before adding Real Markers to the map
deleteMarkers();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
centerPos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
map.setCenter(centerPos);
addMarker(centerPos);
}, function() {
handleLocationError(true, map.getCenter());
});
} else {
//Browser doesn't support Geolocation
handleLocationError(false, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, defaultPos) {
infoWindowContentString = "Sorry, we can't get your location.";
addInfowindow = new google.maps.InfoWindow({
content: infoWindowContentString
});
map.setCenter(defaultPos);
addMarker(defaultPos, addInfowindow);
}
function addMarker(location, addInfowindow) {
var marker = new google.maps.Marker({
position: location,
map: map,
animation: google.maps.Animation.DROP,
icon: yourLocation,
draggable: false,
clickable: true
});
if (addInfowindow == null) {
var myLocationInfowindow = new google.maps.InfoWindow({
content: "Your location"
});
myLocationInfowindow.open(map, marker);
myLocationIconArray.push(marker);
} else {
myLocationIconArray.push(marker);
}
}
function addLocationArrayToMap() {
userPos = new google.maps.LatLng(centerPos.lat, centerPos.lng);
for (var z = 0; z < dabblersArray.length; z++) {
// dabblersArray is an array of Lat & Lng coords.
dabblerLocation = new google.maps.LatLng(dabblersArray[z].lat, dabblersArray[z].lng);
calculateDistance(userPos, dabblerLocation);
markers.push(new google.maps.Marker({
position: dabblersArray[z],
map: map,
icon: dabblers,
draggable: false,
clickable: true
}));
// Some logic to add the distance from the user and the dabblersArray as an alert window above each marker - haven't done this yet.
}
}
function calculateDistance(userPos, dabblerLocation) {
distance = google.maps.geometry.spherical.computeDistanceBetween(userPos, dabblerLocation);
distanceArray.push(distance);
}
JavaScriptには、ユーザーを待たせるという概念はありません。これを実現する方法は、navigator.geolocation.getCurrentPosition
例とまったく同じように、コールバックを使用することです。すでに使用している成功とエラーのコールバックがあります。
そして、あなたはすでに渡しているaddLocationArrayToMap
とgetUserLocation
、それを使用していません。navigator.geolocation.getCurrentPosition
成功ハンドラーのコールバック関数として呼び出す必要があります。任務完了。
function runAll() {
getUserLocation(addLocationArrayToMap);
}
function getUserLocation(onComplete) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
//Your logic...
//Invoke callback
onComplete();
}, function() {
handleLocationError(true, map.getCenter());
});
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加