Drag and Drop to Sort Locations and Render Routes - Medium
文章推薦指數: 80 %
Google Maps Platform, especially the places library and the directions service. Drag and Drop Elements with Sorting. Draggable Elements.
GetunlimitedaccessOpeninappHomeNotificationsListsStoriesWriteDragandDroptoSortLocationsandRenderRoutes:JavaScriptandGoogleMapsAPIPhotobyDanieleLevisPelusionUnsplashThisisawrite-uponthesubjectfeatureoftheTripPlannerappthatIrecentlybuiltfortheFlatironSchool’ssoftwareengineeringprogram.Withthisfeature,userscandraganddropsavedplacesintheirdailyplannerstodeterminetheorderofvisitingeachplace.Basedonthisorder,theappdisplaysdailyroutinganddirectionsonclick.Formoredetailsaboutthisproject,pleaserefertothesourcecode.Afullimplementationofthefeatureinvolvesworkingwith:HTMLDragandDropAPI;andGoogleMapsPlatform,especiallytheplaceslibraryandthedirectionsserviceDragandDropElementswithSortingDraggableElementsFirst,identifydraggableitems,whichwillhaveadraggableattributesetastrue.Theywillalsolistenforthedragstartanddragendevents.Inmyexample,thedraggableitemsaredynamicallyaddedtoalistwhenuserclicksonthesavebutton,likeso:Thefollowingpointsareworthnoting:Makesuretopreservethepace_id(auniqueidassignedbyGoogle),whichisimportantforrouterendering;moredetailsonthislaterDuplicationofdraggableelementscanbeachievedthroughthecloneNode()methodasfollows:Incaseduplicationisneeded,place_idshouldbesavedwiththedatainsteadofidattribute;thisisbecausecloneNode()alsoclonestheelement’sid,whichneedstobeuniqueontheDOMAssigndragstartanddragendEventListenersInmycase,thedragstartanddragendeventlistenersaredelegatedfromtheentirepaneltoindividuallistitems.Itisnecessaryfortheduplicationfeature.ThecloneNode()methoddoesnotcloneeventlistenersfromtheoriginalelements.Ifduplicationisnotneeded,youcanaddtheeventlistenersontheitemasyouappendittotheparentelement.TheabovecodeallowsforthetargetingofthesingleelementthatisbeingdraggedontheDOMthroughtheclassnameof“dragging”,whichfacilitatesstylechangesanditemsortinginthereceivingcontainerwhendragends.AssigndragoverEventListenerstoContainersIdentifycontainersthatwillreceivedraggableelementstoassigndragovereventlisteners,likeso:Iprovidecommentsintheabovecodetoexplain,especially,thegetDragAfterElement()function,whichinvolvescomparingtheYcoordinatesofthedraggingelementtothoseoftheexistingelementsinthereceivingcontainer.Thereduce()functionwithingetDragAfterElement()returnstheelementwhosemiddlepointthedraggingelementisimmediatelyabove.Ifthecodeisunclear,pleasetestwithconsole.loggingtheyandoffsetvalues.Thisconcludestheactionsfordraganddropwithsorting.ThebelowsectionexplainshowtoworkwiththeGoogleMapsPlatformtorenderroutesforagivenday’slocations.RouteandDirectionsRenderingSetupGoogleMapsAPISignupfortheGoogleMapsPlatform,whichprovidesatrialperiod.PleaserefertoGoogle’sdocumentationonhowtogetanAPIkey,enableservices,andrestrictAPIcallaccess.MakesuretoenablethefollowingAPIs:MapsJavaScriptAPIPlacesAPIDirectionsAPIAftereverythingissquaredawaywithGoogle,includethefollowingscripttaginyourindex.html’shead.
延伸文章資訊
- 1A draggable Google Map marker that auto locates and ...
A draggable Google Map marker that auto locates and updates on maker drag - Google Map Marker Lat...
- 2Drag and Drop Markers in Google Maps using JavaScript
The Google Maps is populated using an array consisting of some locations. In order to make the ma...
- 3Draggable Directions | Maps JavaScript API
Google Maps Platform · Maps Platform ... This example demonstrates the use of the DirectionsServi...
- 4Google Map With Draggable Marker Example
Today we will learn google map with draggable marker example. Here, we will see how to add google...
- 5How do I change the Google Maps draggable property for the ...
I have set the property draggable to false in the mapOptions, but I don't know how to set this to...