Google Maps V3 setDirections() callback - Stack Overflow

文章推薦指數: 80 %
投票人數:10人

You can bind an EventListener for directions_changed to your directionsDisplay: var directionsService = new google.maps. OnlydayslefttoRSVP!JoinusSept28atourinauguralconference,everyoneiswelcome. Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. LearnmoreaboutCollectives Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. LearnmoreaboutTeams GoogleMapsV3setDirections()callback AskQuestion Asked 11yearsago Modified 3years,2monthsago Viewed 15ktimes 4 1 IsthereaposibilitytogetacallbackwhensetDirections()isexecuted? "> $(document).ready(function(){ vardestination='=$_POST['address'];?>'; vardirectionsDisplay=newgoogle.maps.DirectionsRenderer({draggable:true}); vardirectionsService=newgoogle.maps.DirectionsService(); vargeocoder; varinfoWindowContent='infocontent'; varmap; varmarker; varposition=newgoogle.maps.LatLng(46.80193957664,11.41754150390625); functioncalcRoute(start,end){ varrequest={ origin:start, destination:end, travelMode:google.maps.DirectionsTravelMode.DRIVING }; directionsService.route(request,function(response,status){ if(status==google.maps.DirectionsStatus.OK){ directionsDisplay.setDirections(response); } }); } functioncodeAddress(address){ if(geocoder){ geocoder.geocode({'address':address},function(results,status){ if(status==google.maps.GeocoderStatus.OK){ calcRoute(results[0].geometry.location,position); document.getElementById('gmap_input').value=address; } }); } } functioninitialize(){ if(destination)geocoder=newgoogle.maps.Geocoder(); map=newgoogle.maps.Map(document.getElementById('gmap_map'),{ center:newgoogle.maps.LatLng(46.80193957664,11.41754150390625), zoom:9, mapTypeId:google.maps.MapTypeId.TERRAIN }); directionsDisplay.setMap(map); directionsDisplay.setPanel(document.getElementById("gmap_directions")); varautocomplete=newgoogle.maps.places.Autocomplete(document.getElementById('gmap_input')); autocomplete.bindTo('bounds',map); varinfowindow=newgoogle.maps.InfoWindow({maxWidth:240}); varmarker=newgoogle.maps.Marker({ position:position, map:map, title:"Title" }); infowindow.setContent(infoWindowContent); infowindow.open(map,marker); if(destination)codeAddress(destination); google.maps.event.addListener(marker,'click',function(){ infowindow.open(map,marker); }); google.maps.event.addListener(autocomplete,'place_changed',function(){ varplace=autocomplete.getPlace(); if(place.geometry.viewport){ map.fitBounds(place.geometry.viewport); }else{ map.setCenter(place.geometry.location); } calcRoute(place.geometry.location,position); }); google.maps.event.addListener(directionsDisplay,'directions_changed',function(){ $('#article').mCustomScrollbar('vertical',350,'easeOutCirc',1.05,'auto','yes','no',false); }); } google.maps.event.addDomListener(window,'load',initialize); $('#gmap_map').css('height','310px'); $('#articleform').submit(function(event){event.preventDefault();}); }); javascriptjquerygoogle-mapscallback Share Improvethisquestion Follow editedAug30,2011at6:30 DIDi askedAug29,2011at9:33 DIDiDIDi 4311goldbadge11silverbadge44bronzebadges 1 excellentexplanationandanswer: – Kappa Feb15,2013at11:45 Addacomment  |  1Answer 1 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 5 YoucanbindanEventListenerfordirections_changedtoyourdirectionsDisplay: vardirectionsService=newgoogle.maps.DirectionsService(); vardirectionsDisplay=newgoogle.maps.DirectionsRenderer({'map':map}); //... varrequest={ origin:..., destination:..., travelMode:google.maps.DirectionsTravelMode.WALKING }; directionsService.route(request,function(response,status){ if(status==google.maps.DirectionsStatus.OK){ directionsDisplay.setDirections(response); } }); //... google.maps.event.addListener(directionsDisplay,'directions_changed',function(){ //...CALLBACK }); Isthatwhatyouwereafter? Share Improvethisanswer Follow editedJul11,2019at10:13 answeredAug29,2011at10:56 polarblaupolarblau 17.4k77goldbadges6262silverbadges8484bronzebadges 6 Thiseventisfiredwhenthedirectiononthemapischanged.Ineedthechangeofthedirectionspanel.Whichissetby"directionsDisplay.setPanel(element);". – DIDi Aug29,2011at11:07 sry,thepanelisdefinedby"directionsDisplay.setPanel(element);" – DIDi Aug29,2011at11:17 Shouldn'ttheychangeatonce? – polarblau Aug29,2011at11:29 Maybe,myproblemis,thatihavetoreloadajQueryScrollpluginbecauseofthedirectionspanel.Andthisdoesn'tworkwiththeseevents.Ithinkthattheeventisfiredbeforeeverythingisloaded,sotheScrollplugindoesn'tgetsthenewcontentheight... – DIDi Aug29,2011at12:37 checktitagainanditwasthedelay,don'tknowwhatididwrongfirstwithit... – DIDi Sep6,2011at13:35  |  Show1morecomment YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedjavascriptjquerygoogle-mapscallbackoraskyourownquestion. TheOverflowBlog Fiveninesuptimewithoutdeveloperburnout(Ep.488) WehateScrumandAgile…whenit’sdonewrong(Ep.489) FeaturedonMeta RecentColorContrastChangesandAccessibilityUpdates Revieweroverboard!Orarequesttoimprovetheonboardingguidancefornew... ShouldIexplainotherpeople'scode-onlyanswers? Linked 8 Googlemapssavingdraggabledirections 1 Detectingtheendofexecutionofanasynchronousfunctionthatprovidesnocallback -1 HowtoknowwhendirectionsServicereturnsit'sstatus? Related 798 Whatisacallbackfunction? 976 HowcanIpassaparametertoasetTimeout()callback? 4376 WhydoesGoogleprependwhile(1);totheirJSONresponses? 714 GoogleMapsJSAPIv3-SimpleMultipleMarkerExample 14 GoogleMapsAPIv3:Clickeventsnottriggeredinfirefoxforcustommarker 2 Redrawingmapbasedonlatandlonggooglemap 0 googlemapapiwithoutrefresh 1787 Howtoaccessthecorrect`this`insideacallback 0 Cannotmodifyvaluewhenuserautocompletegoogleapi HotNetworkQuestions "Kidsthesedays" WhendidHandleTiesbecomemandatoryforMWBC? Whywouldasocietybematriarchalbutpatrilinear? Malepluginstalledinwall Whyissendingtroopsdifferentfromsendingmilitaryequipment? mainwatersupply'sgroundingwires Optimaldepthforundergroundflyby? Asquarenumberthatisaconcatenationofatleast25squares SIngle-directionallinkedlistinC Whatdoyoucallitwhenyouaresayingsomethingpalpablyuntrueonpurpose? Whydolawyerswritecontractswithlanguagethatismoredifficulttounderstandthanotherworks? Doesanyoneknowthetitleandauthorofthisbookwhereaboy/youngmangoesintoaroutineoperationandthesurgeonsdiscoverthathe'sarobot? CanIreplace26inchtireswith29inchtiresonmyGiantSedona? Advantagesofalawlicenseotherthantheabilitytopracticelaw Howtoaddressteammemberwhoexcessivelydeferstomeandisunwillingtogiveclearfeedback Arrowtoanequalsymbolinaequationtojustifyit Frenchequivalentoftheidiom'flogadeadhorse' Howdoesthislibgcryptelgamaldecryptionwork? Whatisthisrhythmgame? Necessityofdecodinginfault-tolerantquantumcomputation Whowerethetwolargehobbits? Emailaddressesgivinginternetconnection Howcanonedistinguishbetween"EthnicRussians"and"mostlyRussian-speakingEthnicUkrainians"? Doespsychologicalhealthrelatetocognitiveability? morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-js Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?