>> I've said that already, but Azure Maps is not
just about a map that you visualize stuff on;
it's also about the intelligence
that you can build with the service itself.
Jim, actually one of our Cloud advocates,
is here on the IoT Show today
to show us how to implement geofencing
algorithms and solutions with Azure Maps.
[MUSIC]
Hi, everyone, you are watching
the IoT Show. I'm Olivier, your host.
Today, we're going to talk about Azure Maps
and geofencing, and we have Jim.
Jim, welcome to the show today.
>> Thank you very much. Thank you.
>> So Jim, tell us a bit about yourself.
Who are you? What are you doing at Microsoft?
>> So I'm what's called a Cloud advocate,
so I'm part of the operations team,
and my job is to help people be successful with Azure products,
whether it's coming to shows like this to present
content, whether it's helping with blog posts,
videos, documentation, going out doing events,
anything I can do just to help people be
successful with the amazing products that we build here.
>> Awesome. Today, you came in
with a proposal for an [inaudible] which is about
using Azure Maps for building
application that leverage the principle of geofencing.
>> Yes.
>> Lots of people don't know what it is.
The other aspect of it which is interesting is that
Azure Map is perceived that the mapping is
like this thing that you're going to have
this graphics and so on to
visualize buildings, routes, and so on.
But actually, Azure Maps is about much more than just that.
We'll talk a bit about that as well.
>> Yes. Azure Maps is a whole suite of services.
It's not just the control and screen;
it's a horde of spatial-based services to understand the world's
GPS coordinates and just [inaudible] interactions with those.
Now, geofences are a way of defining on a map a certain area
and you define that
using standards; latitude/longitude-type coordinates.
>> Okay.
>> Then what you can do is you can test whether
or not a point is inside or outside the geofence.
>> Okay.
>> So for example, if I just refer you to a map
here and bring up the little drawing tool,
you could imagine you'd want to define
a geofence, say, around the Microsoft campus.
>> Okay.
>> So I could say I've got a series of lines here that
define a border around the campus.
These are defined as polygons
and each of these points that define
the polygon
are GPS coordinates of latitude and
longitude. So measured from around the world, it
goes up to 90 down to minus 90, and then this way,
it's minus 180 to 180.
So each of those corners defines a polygon
and that then defines your shape.
>> Your zone, yeah.
>> Yeah. Now, the standard way of defining these
is with a thing called GeoJSON.
>> Okay.
>> So I've got to say I hit the trace from GeoJSON,
and literally it is just a series of points.
>> Of points.
>> So I've got a square here and I have five points.
Normally, we had defined squares, not five, it's four,
but the idea is the last point closes the square.
So last point is the same.
So I've got these around the area of Redmond where minus
122 longitude, 47 latitude, and then all the artifacts is there.
>> Okay. Interesting.
>> So once I've got my geofence,
I can then send that to
Azure Maps, [inaudible] the Azure Maps service, and then
I can send queries to Azure Maps to say am I
inside or am I outside the geofence.
>> Okay.
>> So let's just look at some code to do that now.
I've got an Azure Maps instance I just spun up in Azure.
>> Okay.
>> I'm using VS code here and I built some Python code
that uses the REST APIs that come with Azure Maps.
>> Okay.
>> First, we want to upload a geofence and then to query it.
So what I've got here is I've got some Python code that I've built
inside Visual Studio code that calls the REST APIs in Azure Maps,
first of all, to upload a geofence, and
then after that, I query that geofence with
points inside and outside to see
the results that you can get out these kind of operations.
So initially, the code I make, I just load up the GeoJSON.
I've got a GeoJSON file here.
>> Okay.
>> It's the same GeoJSON we
saw on that web page earlier.
I take that and I send that JSON up to an upload function
and that then gives me back a location.
That's stored inside Azure Maps and I get back
a URL I can query to download data about that.
>> Okay. Once you've done, that you've uploaded that GeoJSON,
you just do it once, right?
It's not every time you have a query,
you have to upload that JSON.
>> I'll upload this once and that is stored,
and then once I've got the location of where it is,
I can query that location, call back a
unique identifier, and that defines that geolocation.
>> Okay. Cool.
>> Then once I've got that,
I can use my REST API to query
that geolocation and find out am I inside or outside,
and it's just one call.
I make the call, I pass the latitude and
longitude, the unique identifier,
the UDID of my geofence, and that brings back some results.
>> That's it.
>> That's it. Yeah. So [inaudible] very positive on this,
and I will just run this
and we'll see the output that comes out.
I click Run, this would have run my Python code.
Okay. So my first code uploads my geofence.
This is the code that loads that GeoJSON, pushes up into Azure
Maps, and then that should come back with the unique identifier.
So the function's been run and it's returned to me this unique
identifier. So I can now use this ID on all the calls I make.
>> Okay.
>> So the first call I'm going to make is I'm going to check
a geolocation inside the geofence.
>> Okay.
>> So I'll point right in the middle and then make
a call, I'm going to call that ID and I'll give
it those coordinates, and it's come back to
say distance to geofence is minus 999.
>> Okay.
>> I'll come back to that value in a minute, it's kind of
important.
>> Okay. Interesting.
>> But it also defines where on the geofence is
the closest location to my point, [inaudible]
>> Okay.
>> So when I run this again with a point that's outside the
geofence, you'll see this now come out with 999.
>> Not minus?
>> Not minus but positive.
>> Okay.
>> Then now, I've got another call, we're
right on the edge of the geofence.
>> Okay.
>> So I call that and that comes back with minus 6.
Now, this seems a bit weird,
but it comes down to the fact that GPS is surprisingly inaccurate.
>> The public one.
>> Yeah. Even the military ones.
>> Yeah. So the way GPS works, you have a receiver
and that connects to four satellites
orbiting the Earth. It [inaudible] on time signals
and then it measures the length of time
it takes for signals to get from satellite
to the receiver and then use
that to trilaterate its position.
Now, the problem is although the speed of light is fixed,
that signal can get slowed down by atmosphere,
moisture in the air, it can bounce off buildings,
so it's not quite accurate.
So you look into within about 10 meters.
So if I'm on the edge of the geofence,
am I actually in or out?
I could be standing outside but the GPS thinks I'm inside.
So the way this works is when I make the code,
let's go back up the code,
I specify what's called a search buffer.
>> Okay.
>> Anything outside the search buffer
is considered to be inside or outside.
So this is a search buffer of 50 meters. If I'm within 50 meters
of the geofence, then it returns the distance that it thinks. If
I'm more than 50 meters inside or more than 50 meters outside,
it returns minus 999 for inside, 999 for outside.
>> Okay, got it.
>> So minus 999 means I'm definitely inside,
999 means I'm definitely outside,
anything else means I'm within that search buffer.
So what I'd then have to do is start
building logic into my code to say,
"Am I inside? Am I outside?"
You may have seen it, I don't know if you use a satellite in your
car. Sometimes, you're driving along and you got to a fork
and the satellite shows you're still going that way
and then it suddenly jumps over.
>> Yeah.
>> Yes. So it is trying to work out where you are.
It uses accelerometer data, gyroscope data,
it uses a [inaudible] filter
to trying and work out where exactly
you are and make a best guess,
and sometimes, it gets it wrong.
>> Basically, that's a way for you to
tune your accuracy of your results basically, right?
>> Yes.
>> Okay.
>> Yeah. So you think a kind of
example of why you want to use a geofence.
New Zealand, for example, they
have what are called road user charging.
>> Yeah.
>> So when you go fill up with
petrol in a commercial car, you pay tax to the pump.
>> Yeah.
>> When you fill up with diesel, you don't pay tax to the pump.
Instead, you pay tax later
based off the mountain miles you've done on
public roads and the weights of
your vehicle. So in a sense, you're paying for damage that you do.
Now, if you're driving off road,
why should you pay towards
the upkeep for roads because you're driving off-road?
>> Fair enough. You're still polluting though.
>> Still polluting, yes, that is a separate tax issue. But in terms
of the payment for the roads, the road user charging,
if you're driving a logging truck on road, you pay tax.
Then if you enter a geofence which is you're locking
area off-road, you can then enter the geofence,
tag that you've entered the geofence,
stop paying tax while you're driving around,
and then when you leave, you start paying tax again.
>> Okay.
>> Now, sometimes, that logging area is right on a road.
So you need to track that
you've left the road, you're going to a logging area, but
then when you're driving back
near the road but not actually on it,
your GPS may pick up that
you've left the geofence when you haven't.
So that's why we have [inaudible]
of where actually am I.
Am I at an area where I can rejoin the road?
Because I haven't been through
that one exit here, then I might still be-
>> On there. Yeah.
>> Yeah. So you can't just rely on this being 100 percent
accurate because GPS is just fundamentally inaccurate.
>> Awesome.
>> Awesome. Now, what's also really cool about
this geofence stuff is we can wire up events.
So inside Azure Maps, we have
this whole events subscription that puts events on Event Grid.
>> Yeah.
>> So what I can do is when I
make this call to the geofence, I can say,
"Alert when I enter or leave a geofence."
>> Okay.
>> So I've got a bit of code here that will do that,
and then when I actually make this call,
I have to define a device ID.
So every time I call, I have to say,
"This is the device I'm checking."
>> Okay.
>> Then with that particular device,
if I'm outside then outside then inside,
if perhaps I've entered the geofence,
It'll actually raise an event on the Event Grid.
>> Okay.
>> So I actually got some code here that does this.
I've got outside, outside, inside.
>> Okay.
>> If I run this code,
what it will do is we'll be outside,
we'll be outside, be inside,
an event will then get fired
inside the Azure Maps through the Event Grid
and that can be fed to any kind of system.
So what I can do here,
I've got one here,
so 999 outside, Event published, False,
999 outside, of Event published, False,
minus 999 inside, Event published, True,
that has published an event onto the Event Grid.
>> Okay.
>> Then what I can do with these events,
so I will have a logic app. If I
want to build like a no-code-type scenario,
I could have a logic app to this.
Then in this particular example here,
when the event is found in the Event Grid,
I can have things like a webhook,
and from a webhook, I can then connect to some logic app.
So here, I've got a logic app here that when
the webhook is called, it'll post a tweet.
So now, when I've called outside, outside, inside,
in theory, if I go to my Twitter account, you
should see under my tweet,
"My geofence has been entered.
Test tweet using Azure Maps and logic apps."
>> Nice.
>> So just by calling the geofence, I've published a tweet.
Obviously, that was a bit of an amusing scenario
but you think you can put this to send notifications.
A good example of this
is if you have expensive machinery with
a location tracker on there which is in a warehouse somewhere,
if that leaves that warehouse, you get notified,
because it could have left the warehouse because it's
going on site or it's been stolen.
>> Yeah.
>> So the fact you can wire this kind of capability in
when a truck is entering an area, you get a notification via
push notifications so you'll [inaudible]
>> Safety as well. You're talking about security [inaudible]
safety is one of the major scenarios where you say, "Hey,
I don't want that employee or
that machine to enter that zone because of that risk
of fire or whatever" and then
you have it with the geofencing capabilities in the maps.
>> So you have an area to say,
"I don't want any employees going to this area during
this particular period because something is happening."
Employees have locations trackers on them,
and then if they do enter the area
that breaches the geofence, you get an alert,
you can call that employee and say "Please leave here."
>> Awesome. Nice introduction to geofencing with Azure Maps.
Jim, thanks a lot for that one.
>> Thank you.
>> If someone wants to learn more, actually, they can go
to aka.ms/IoTShow/AzureMapsgeofencing and this
is your link to documentation on more tutorials
and more information about using geofencing in Azure Maps.
Jim, hope to see you soon on the IoT Show again.
>> Thank you.
>> Everyone, thanks for watching and don't forget to subscribe.
[MUSIC]