mapkit js - pass user location for directions origin

Good day. To preface my ignorance, I have only just begun using any development tools. If you need an enterprise WAN/LAN put together, I'm your guy, writing code, not so much.


To that point, I am attempting to gather a user location to be passed to a directions request as the origin. Based on the sparse documentation and WWDC videos I have this mnuch:


<snip>

let request = {

origin: "<name of a place>",

destination: annotation.coordinate,

requestsAlternateRoutes: true

};

</snip>


The destination is a user selected annotation from the map. The query functions fine. I cannot figure out how to grab the user coordinates (even with geocoder) to be turned into a "place" per the documentation.


If anybody has seen/solved this, I would be greatly interested.


Thank you in advance.

Replies

This can be done in a couple steps:


First, you will need to update your map to show user location:

https://developer.apple.com/documentation/mapkitjs/mapkit/map/2973942-showsuserlocation


Once you have set showsUserLocation

to true, your map will display the user's location with a blue annotation. Now that you have a user location annotation you can use its coordinate, similar to how you are setting the destination. You can access the user location annotation here:


https://developer.apple.com/documentation/mapkitjs/mapkit/map/2973949-userlocationannotation


The browser will request access to the user's location when the map initializes. This is a browser security feature.

Eric,

Thank you for your time. Would any additional documentation exist? I cannot seem to get this worked out.


I have both:

map.showsUserLocation = true;

map.showsUserLocationControl = true;


And see the blue annotation on the map. However, I just can'tseem to pass that to the origin in:


let request = {

origin: "<hard coded city name>",

destination: annotation.coordinate,

requestsAlternateRoutes: true

};


This is working as intended (with some route selection issues to work out).


Any additional information is appreciated.