無題
文章推薦指數: 80 %
setMap(map); // point the directions to the container for the direction details ... display the direction details in the container directionsDisplay. vardirectionDisplay,map; vardirectionsService=newgoogle.maps.DirectionsService(); vargeocoder=newgoogle.maps.Geocoder(); functioninitialize(){ //setthedefaultcenterofthemap varlatlng=newgoogle.maps.LatLng(-25.166802,29.082124); varlatlng1=newgoogle.maps.LatLng(-25.166802,29.082124); //setrouteoptions(draggablemeansyoucanalter/dragtherouteinthemap) varrendererOptions={draggable:true}; directionsDisplay=newgoogle.maps.DirectionsRenderer(rendererOptions); //setthedisplayoptionsforthemap varmyOptions={ zoom:14 , center:latlng, mapTypeId:google.maps.MapTypeId.ROADMAP, mapTypeControl:false, styles:[{"stylers":[{"saturation":-100},{"gamma":1}]},{"elementType":"labels.text.stroke","stylers":[{"visibility":"off"}]},{"featureType":"poi.business","elementType":"labels.text","stylers":[{"visibility":"off"}]},{"featureType":"poi.business","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"poi.place_of_worship","elementType":"labels.text","stylers":[{"visibility":"off"}]},{"featureType":"poi.place_of_worship","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"road","elementType":"geometry","stylers":[{"visibility":"simplified"}]},{"featureType":"water","stylers":[{"visibility":"on"},{"saturation":50},{"gamma":0},{"hue":"#50a5d1"}]},{"featureType":"administrative.neighborhood","elementType":"labels.text.fill","stylers":[{"color":"#333333"}]},{"featureType":"road.local","elementType":"labels.text","stylers":[{"weight":0.5},{"color":"#333333"}]},{"featureType":"transit.station","elementType":"labels.icon","stylers":[{"gamma":1},{"saturation":50}]}] , scrollwheel:false }; map=newgoogle.maps.Map(document.getElementById("map_canvas"),myOptions); varcontentString1=''+ ''+ ''+ 'MaatsebeCountryLodge'+ ''+ ''+ ' R573MolotoRoad,NexttoMoutsiyaMall'+' 0736808051'+' [email protected]'+' ViewinGoogleMaps'+''+ ''; varinfowindow1=newgoogle.maps.InfoWindow({ content:contentString1 }); varcustomMarker1=newgoogle.maps.Marker({ position:latlng1, map:map, title:"MaatsebeCountryLodge", icon:{ path:fontawesome.markers.MAP_MARKER, scale:1.3, strokeWeight:0.6, strokeColor:'#000', strokeOpacity:1, fillColor:'#753e08', fillOpacity:1, anchor:newgoogle.maps.Point(20,0), } }); customMarker1.addListener('click',function(){ infowindow1.open(map,customMarker1); }); //bindthemaptothedirections directionsDisplay.setMap(map); //pointthedirectionstothecontainerforthedirectiondetails directionsDisplay.setPanel(document.getElementById("directionsPanel")); } functioncalcRoute(){ //getthetravelmode,startpointandviapointfromtheform vartravelMode=$('input[name="travelMode"]:checked').val(); varstart=$("#routeStart").val(); varend=$("#routeEnd").val(); //composeaarraywithoptionsforthedirections/routerequest varrequest={ origin:start, destination:end, unitSystem:google.maps.UnitSystem.METRIC, travelMode:google.maps.DirectionsTravelMode[travelMode] }; //callthedirectionsAPI directionsService.route(request,function(response,status){ if(status==google.maps.DirectionsStatus.OK){ //directionsreturnedbytheAPI,clearthedirectionspanelbeforeaddingnewdirections $('#directionsPanel').empty(); //displaythedirectiondetailsinthecontainer directionsDisplay.setDirections(response); }else{ //alertanerrormessagewhentheroutecouldnogbecalculated. if(status=='ZERO_RESULTS'){ alertify.alert('Noroutecouldbefoundbetweentheoriginanddestination.'); }elseif(status=='UNKNOWN_ERROR'){ alertify.alert('Adirectionsrequestcouldnotbeprocessedduetoaservererror.Therequestmaysucceedifyoutryagain.'); }elseif(status=='REQUEST_DENIED'){ alertify.alert('Thiswebpageisnotallowedtousethedirectionsservice.'); }elseif(status=='OVER_QUERY_LIMIT'){ alertify.alert('Thewebpagehasgoneovertherequestslimitintooshortaperiodoftime.'); }elseif(status=='NOT_FOUND'){ alertify.alert('Atleastoneoftheorigin,destination,orwaypointscouldnotbegeocoded.'); }elseif(status=='INVALID_REQUEST'){ alertify.alert('TheDirectionsRequestprovidedwasinvalid.'); }else{ alertify.alert("Therewasanunknownerrorinyourrequest.Requeststatus:nn"+status); } } }); } google.maps.event.addDomListener(window,'load',initialize);
延伸文章資訊
- 1輕鬆搞定Google雲端技術:Maps‧Android‧App Engine‧Cloud SQL與電子商務API實例解析
var directionsDisplay = new google.maps. ... setMap(map); //此物件需要在一開始就初始化 directionsDisplay.setPa...
- 2Directions Service | Maps JavaScript API - Google Developers
- 3Directions for multiple markers on map - Google Groups
directionsService.route(request, function(response, status) { if (status == google.maps.Direction...
- 4draw route between two locations in google map | FULL SCRIPT
- 5Plot a Route on Google Maps | Embed directions map on website