Maps (Xamarin.Essentials API of the Week)

>>On this week’s
Xamarin.Essential API of the Week.
We take a look at the Map
API to launched users
directly into a MapPoint or to
start navigation on their device.
>>Welcome back everyone to another
Xamarin.Essential API of the week.
Today we’re going to
take a look at Maps,
which is enabling developers such as
yourselves to open
the default Maps application.
Now you can do to a specific point
on the Map or you can start
navigation like walking,
driving, transit,
all sorts of different things
to launch Google Maps,
Apple Maps or Bing Maps,
or whatever Maps application
your user has.
So, let’s hover to Visual Studio
and see how to do it.
Now of course, I always
recommend going to and tap on
the Xamarin.Essential
API Documentation.
When you go there,
you’re going to find
every single API guide for
every feature inside
of Xamarin.Essentials.
So, whether you’re using Launcher,
MainThread, open browser
or Maps, there it is.
Now to get started of course,
we’re going to take
a look at the Maps API.
It’s really simple.
Of course we’re going to start by
using Xamarin.Essentials over here,
and you can even copy
and paste the code.
Then we can see
the different types of
information that we’re
going to need to
pass into the open Map call
to launch the Map app.
So, for instance here, we
can launch it by passing in
a location or we can do it
by passing a place mark.
So location on the Map.
So, let’s head over to
Visual Studio and take a look here.
So, here’s my Essential
sample application
that I’ve been using as part of
the Essential API of the Week.
I have my Android iOS
and.NET standard project,
and I’ve installed
Xamarin.Essentials into all of them.
I also followed that
getting started guide,
and in my main application
of my Android app,
I’ve added a little bit
of setup code for
permissions and also a way
to initialize Xamarin Forms,
that’s all I needed to do.
So, we’re going to
enable our users to
enter the latitude and longitude,
and optionally a name to
launch the Maps application.
So, here on the Maps page,
I have a simple entry for the name
and entry for the latitude
and longitude.
I’ve also put some defaults here,
which are the Space Needle
and Downtown Seattle.
Then I also have a button
that when clicked
will execute a way of
opening up the Maps app.
Now, if I go into the code
behind, I haven’t done much.
All I’ve done is done a little bit
of TryParsing to get strings
into doubles that we’ll use for
the latitude and longitude.
So, the first thing we’re
going to do is go using
Xamarin.Essentials to bring
in all of those APIs.
Then down here, once we have
our latitude and longitude,
we can just simply type in Map.
and we see OpenAsync.
Now inside of OpenAsync,
we’ll notice,
if I go ahead and zoom
in for us here that we
can pass in a location,
a placemark or a
longitude right here.
Now it’s nice as that I
have the option to not
only do those different parameters,
but also these MapLaunchOptions.
So, let’s go ahead and
just say, “lat, long”,
and we’ll see that it will
default to the latitude,
longitude, and these Map options.
So, I’ll go ahead and create
a new MapLaunchOptions.
So, you can take a look
and see what’s inside.
By default we have a Name
and a NavigationMode.
So, for Name, I’m going
to do EntryName.Text,
so whatever we enter.
Then NavigationMode gives
us the option of cycling,
default, driving, none at
all, transit or walking.
So, let’s just open it
up and say, “None”,
just don’t put it in
a NavigationMode at all.
I just want to launch to
that point on the Map.
Now, this is an async call,
so we’ll go ahead and await it.
We’ll go ahead and put an async flag
on top of here. There we go.
With just a few lines of code,
we should be able to
launch our Map application
from the code right here.
So, let’s go ahead and
relaunch it over on
our Android device or I have
Google Maps installed. Here we go.
So, we’ll give it a few seconds to
recompile and then get
it up on the screen.
Here we go. We’ve launched.
So, I can go into my Map section.
Here I will go ahead and
type in space needle.
I can see my latitude
and longitude there,
and then I’ll say,
“Open coordinates.”
Now I have multiple
applications installed.
So, I’m going to go ahead and
select Google Maps and go ahead,
and just with that one click, boom,
it go ahead, opens up
Google Maps and we can
see that it even says,
space needle right
there on top of it.
Now, since I’d put it
in no NavigationMode,
we can say I have the option
to do directions,
sharing or save to
that specific point.
Now, at the same time what I’m
able to do is say, you know what?
I really want that to be
driving as the default.
So now, if I go ahead and relaunch
this application or maybe
I want to have my user
navigate to an office or
just some structure or
to somewhere in
Downtown Seattle for instance,
they’ll go ahead and start
navigating directly.
So, let’s go ahead and
see this now into Maps.
I’ll say, “Open coordinates”,
just opened it up by default here.
It’ll say welcome to navigation,
finding the best route
for me automatically.
Boom, I’ve launched directly into
Maps and I’m navigating from
beautiful building 25 in Redmond
Washington all the way Downtown.
As we can see, I may
decide to take the
520 and pay a toll
or take the 90 even
though it’s four minutes slower
or take the 420 to
save a few dollars.
There you have it in
just a few lines of code,
we’ve integrated Xamarin.Essentials
into our app and
the Map API to guess
what? Launch a Map.
Hopefully, you enjoyed this video,
check out all the
documentation and only
launch it with these
latitude and longitude.
But also the placemark for
a direct location with an address,
and check out all the other videos
in the Xamarin.Essentials
API of the Week.
I hope that you enjoyed
it. If you have
any questions, feel
free to reach out.

Leave a Reply

Your email address will not be published. Required fields are marked *