Saturday 2 February 2019

TravelTimeCalculator: Adding Multiple Markers for Directions

TravelTimeCalculator shall illustrate the best travel mode for different coordinates, related to the target of the map. I'll add the markers to an array of markers, either when the user taps the map or when the user moves the map to a new location.

Adding Markers After Tap

I store the markers in an array of markers.

Step 1: Adding markers dynamically when user taps on map
I added two lines of code to the onMapClicked method:

Step 2: Changing the added markers
CheckDirectionsFromDb will check if the direction has data for all transport modes. In that case, it will call updateMarker for that coordinate:
Since the method doesn't care whether the center of the map equals orig or dest,
I'll let the method updateMarker do that check,
To find the marker to change, the app searches all existing markers and compares with the tapped location:
First, the script determines which coordinate is connected to the marker.
After that, it changes the connected icon. Note the icons to the right.
The result is markers added to the map.

For some strange reason, the title is hidden when another marker is added.

The next blog post will discuss how the program shall add when moving the map. The markers should be cleared and existing directions shall be added, if found in the database.

No comments:

Post a Comment