Apple Maps Direction Turn by Turn IOS

Hii all,


First, I'm working on create maps navigation App using Apple Maps. This maps will show line for specific route. The problem is that possible to show Turn By Turn direction flags just for "Turn Left" or "Turn Right" etc so i can show the image for the direction. Because based on my code bellow, in the response i just found "Instruction" field that contain full instruction sentence like "Turn right onto ABC Street". So i cannot get only direction for showing the image.

If i see on google maps direction API, the results have field name "Maneuver" thats contains only "Trun-left" sentences. So where i can find that in Apple Maps API ?


I Already using MKDirections for getting route information.

var request = new MKDirectionsRequest

{

     Source = sourceItem,

     Destination = destItem,

     RequestsAlternateRoutes = false,

     TransportType=
MKDirectionsTransportType.Automobile

};

try {

var directions = new MKDirections(request);

var response = await directions.CalculateDirectionsAsync();

if (response != null) {

        // i get the results API in response

}

}

What's exactly your problem ?


- Show image of turn left instead of text ?

You can probably use a trick : include image in the text or replace text with image:


look at details here:

h ttps://www.hackingwithswift.com/example-code/system/how-to-insert-images-into-an-attributed-string-with-nstextattachment


- Or is it to analyze the answer just to get the instruction "Turn Left" ?

In that case, should be even simpler : just get the first 2 words out of the reply and you get the message you want.

Hi Claude31,

My problem is how to show the image of direction using data from the API. I found this set of image for direction https://i.stack.imgur.com/yYm2Y.png

First, how i decide which image to show for Turn By Turn Navigation for every corner Base only instruction sentence like "Turn right onto ABC Street".

Second, I cannot only using string matching because i must handle localization for every language.

To handle localization, one way should be to define your own directions:


let turnLeft = NSLLocalizedString("turn left", comment: "left")
let turnRight = NSLLocalizedString("turn right", comment: "right")

// etc

// I write lowercase to ease further comparison


Then, you should test which string is contained in instructions (do the comparison in lowercase)

var dirID = -1 // Not yet found
if instructions.lowercased().contains(turnLeft) {
     dirID = 0     // This must correspond to the image number you will have defined for turn left
} else
if instructions.lowercased().contains(turnRight) {
     dirID = 1
}
// continue tests


The select the image to put in the message

Thanks for the suggestion..


For language, MKDirection always return the instruction language based on Device language.

Is that possible to set parameter to handle language return?So i only receive return for English or Dutch language?

I think that language returned depend on set up of the phone itself.


I don't know if Xamarin provides the index oth the reply. If not, may be you could use a trick :

- create an array with the road signs images you want

signs[0] = the left pointing arrow

signs[1] = the right pointing arrow


- select on a map a point (by its coordinates) and elect four detination points, one that will force to turn left, another to turn right, …

- then, in applicationDidFinishLaunching, call

response = await directions.CalculateDirectionsAsync();

for each the the four destination points.

- you will get answers in the iPhone language, as:

Turn right onto ABC Street

Tournez à gauche dans la rue ABC

- keep the sentences in an array[String]:

indication[0] = "Tournez à gauche dans la rue ABC"

indication[1] = "Tournez à droite dans la rue XYZ"

- when you get direction in a later call to directions.CalculateDirectionsAsync, you will get answers in the same language ; then find which sentence of your array has the longest matching start with this answer.

if you get "Tournez à droite vers UVW"

This matches 9 chars with indication[0] and 15 with indication[1]

- you will know that the direction is 1 ; then you can post the correct sign : signs[1]

Apple Maps Direction Turn by Turn IOS
 
 
Q