ユーザーのジオロケーションを取得するスクリプトを作成しようとしています-ユーザーがそれを有効にしている場合は、事前定義された目的地へのルートをプロットします。ジオロケーションが有効になっていない場合は、事前定義された場所をプロットするだけです。スクリプトは機能していませんが、コードを調べることで、私が何をしようとしているのかをよく理解できるはずです。私は正しい方向に進んでいますか?なぜそれが機能していないのか誰かが見つけることができますか?
<script type="text/javascript">
$(function (){
var dest = "Unit 20, Tallaght Business Centre, Whitestown Road, Tallaght Business Park, Ireland";
if(geolocEnabled()){
getLocation();
}else{
plotMarker(dest);
}
//check if geolocation enabled
function geolocEnabled(){
return navigator.geolocation;
}
//plot marker for VRF office
function plotMarker(dest){
$('#map').gmap3(
{ action: 'addMarker',
address: dest,
map:{
center: true,
zoom: 14
},
marker:{
options:{
draggable: false
}
}
}
);
}
//get user's location
function getLocation(){
$('#map').gmap3(
{ action : 'geoLatLng',
callback : function(latLng){
if (latLng){
plotRoute(latLng, dest);
return;
} else {
alert("Unable to determine your location. Enable geolocation services and try again, or consult the map for our location.");
plotMarker(dest);
}
}
});
}
//plot route
function plotRoute(latLng, dest){
$('#map').gmap3(
{ action:'getRoute',
options:{
origin: latLng,
destination: dest,
travelMode: google.maps.DirectionsTravelMode.DRIVING
},
callback: function(results){
if (!results) return;
$(this).gmap3(
{ action:'init',
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: true,
center: [53.337433,-6.2661]
},
{ action:'addDirectionsRenderer',
panelID: 'directions-panel',
options:{
preserveViewport: true,
draggable: false,
directions:results
}
}
);
}
}
);
}
});
</script>
すべてが大いに感謝するのに役立ちます。
編集:スクリプトを実行しても、ブラウザ内でジオロケーションの警告が表示されません。
編集:getLocationから{timeout:10000}を削除しましたが、アラートが表示されるようになりました。スクリプトが更新されました。