# (13) - HERE Geofencing - Location Monitoring

Because of COVID-19, geographic fencing has become a well-known service, because such technology is used to monitor whether a home quarantined subject has left the residence.
Generally speaking, when we talk about geographic fences, we usually imagine a certain area, whether square or polygonal, but HERE's geographic fences can exist in the form of points, lines, and planes (polygons), so It is more flexible in use.
Taking the example of the delivery of goods just now, in addition to monitoring whether the driver has arrived or left the customer’s factory, I can also set the delivery route as a geographic fence, so I can also monitor whether the driver has deviated from the delivery route. , This use is very helpful for high-value goods (such as money transport vehicles), and personal safety monitoring (such as pick-up and drop-off of important persons, taxis, child safety monitoring, etc.).
### HERE Geofencing functions and features
As previously described in [Quickly Construct Map Service (12)\- HERE Custom Locations Map Data Storage and Query](https://ithelp.ithome.com.tw/articles/10246323), the backend of HERE Geofencing is actually Custom Locations, it can also be said that Geofencing is an extended application of Custom Locations.
Suppose you are currently making an APP, this APP will be installed on the child’s watch, and the watch will report the GPS location to the cloud control center every minute, and the control center will check whether the GPS location is in a specific area set by the parent , Such as home, relatives, security classes, school, etc. With the service of HERE Geofencing, every query will tell you whether the GPS location is within a specific fence, and if it is not in the fenced area, it will also tell you how far it is.
### Precautions during implementation
Of course, from the perspective of HERE, it does not limit the user's usage, because the more you use, the more HERE is of course the better (the more you charge); however, in practice, you will need to consider the power of the device (if it is Wearable devices or GPS trackers, the battery is usually not too big) and data flow (for GPS trackers, the capacity of the SIM card above is usually quite limited), so it is more recommended that you can set the query frequency for different scenarios.
For example, looking at the scene of the child monitoring just now, generally we can set the query interval of the class time to be extended, because if the child is nowhere to be seen during class time, the first thing that will be found is the school, and the school will be activated immediately Search and inform the parents; however, it is necessary to shorten the interval of inquiries during the end of class, especially the journey from after class to before returning home is the most critical. You can set different time points and locations on the device to query at different time intervals.
### HERE Geofencing first experience
#### Make a geographic fence
As mentioned earlier, the backend of HERE Geofencing is the same as Custom Locations, so when you set up a geographic fence, you must use the format accepted by Custom Locations.
First open QGIS, add a new project, then add OpenStreetMap to the bottom map, and then Zoom In to your preferred area.

Then we add a Shapefile, please select "Layer" -> "Create Layer" -> "New Shapefile Layer..." in the menu.

Then, in the "File name" section, you can name your preferred name. In the "Geometry type", please select "Polygon", which means that we will add a polygonal geographic fence later.

Next to the New Field section below, we add a new field called "name", enter "name" in "Name", and click "Add to Fields List" below to add this new field to what we want In the newly added Shapefile.

It will look like this after completion, just press "OK".

Then we will see a new layer in Layers, which currently has no content. Please right-click on it and select "Toggle Editing" to open the editing mode.

Then we can find this "Add polygon" button on the toolbar, press it, and the cursor on the map will turn into a crosshair.

Then we found a elementary school on the map, and clicked the left mouse button on each corner of the elementary school in order. You can see a light red frame covering the elementary school campus.

After confirming that the entire campus is covered, please click the right mouse button. At this time, a dialog box will appear. Please enter the name of the polygon in the "name", such as "Daxin Elementary School", and then click "OK". Up. The new polygon will appear on the map.

Then please add a few polygons around in the same way. For example, I have added "Park", "Carrefour", "Internet Cafe", "Library", "Anqing Class", "Grandpa's House", "Home" and other places here.

Okay, then we right-click on the newly added polygon layer and select "Export" -> "Save Features As...".

Then as we described in [Quickly construct map service (12)\- HERE Custom Locations map data storage and query](https://ithelp.ithome.com.tw/articles/10246323), select "Format" "CSV" and save the file to your preferred location. But please remember that the encoding must be "UTF-8", "GEOMETRY" must be "AS_WKT", and "SEPARATOR" must be "TAB". After confirming that it is correct, press "OK" to save the file.

Open the CSV file you just saved. The first field is WKT, and it also contains a string of text starting with MUILTIPOLYGON. This is a polygon in WKT format. We will use this file to upload as our geographic fence.

Of course, the way of using QGIS is a bit more manual. In practice, you may ask the user to use the phone screen to click or use the position plus the radius to generate a polygon that is approximately circular to set a range, which can generate There are many ways of polygons, and QGIS is just for demonstration.
The part of QGIS ends here, you can turn it off. **But the first step here is that you must change the extension of the CSV file you just saved from ".csv" to ".wkt". **
#### Upload geographic fence
Then open Postman, add a POST request, the URL is "`https://fleet.ls.hereapi.com/2/layers/upload.json`", and add two parameters in "Params":
* apiKey: Your HERE API KEY.
* layer_id: fill in "MYGEOFENCE"

Then in the "Body" section, add a "Key" as "zipfile", select "File" to the right of "Key", and then click "Select Files" under "Value" to select the WKT you just saved files.

Then you can click "Send" to upload. If all operations are correct, you will receive a 201 postback to indicate that the upload was successful.

As for if you want to add, delete or modify your geographic fence in the future, please follow the [Quick Construction of Map Service (12)\- HERE Custom Locations Map Data Warehousing and Inquiry](https://ithelp.ithome .com.tw/articles/10246323).
#### Query geographic fence
Then we open [HERE WeGo Network Map](https://wego.here.com/?map=23.70099,122.10814,8,traffic&x=ep), move to the vicinity of the location where you just created the geographic fence, and then click Press the right button at random, a string of addresses will appear, and then press that string of addresses.

Then in the upper left corner, please click "View More Information".

The latitude and longitude of this address will appear.

Then please go back to Postman, add a GET request, paste "`https://fleet.ls.hereapi.com/2/search/proximity.json`" in the address bar, and add the "Params" below Three parameters:
* apiKey: Fill in your HERE API KEY.
* layer_ids: Fill in "`MYGEOFENCE`", which represents the name of the layer you want to search.
* Proximity: Fill in the latitude and longitude you have just obtained from the HERE WeGo map, followed by ",1000" to search for geographic fences within 1000 meters, such as "24.14405,120.64769,1000", please remember not to leave a blank .
Please click "Send" after confirming that it is correct.

If everything is correct, we can get a back pass. In the array of geometries, it contains the geographic fences within the search range of 1000 meters, and the distances will be sorted out in order.
For example, for the first "Daxin Elementary School", the distance (distance) is 103.03 meters, and the latitude and longitude of the geographic fence closest to the search location of "Daxin Elementary School" is 24.14498, 120.64771.
```json=
{
"geometries": [
{
"attributes": {
"ID": "",
"GEOMETRY_ID": "0",
"NAME": "Daxin Elementary School"
},
"distance": 103.03,
"nearestLat": 24.14498,
"nearestLon": 120.64771,
"layerId": "MYGEOFENCE",
"geometry": "MULTIPOLYGON(((120.64697 24.14669,120.64802 24.14668,120.64799 24.14497,120.64693 24.14499,120.64697 24.14669)))"
},
{
"attributes": {
"ID": "",
"GEOMETRY_ID": "4",
"NAME": "Park"
},
"distance": 119.7,
"nearestLat": 24.14406,
"nearestLon": 120.64651,
"layerId": "MYGEOFENCE",
"geometry": "MULTIPOLYGON(((120.6439 24.14674,120.64654 24.1467,120.64651 24.1439,120.64328 24.14422,120.6439 24.14674)))"
},
{
"attributes": {
"ID": "",
"GEOMETRY_ID": "6",
"NAME": "Anqin Class"
},
"distance": 172.07,
"nearestLat": 24.14295,
"nearestLon": 120.6465,
"layerId": "MYGEOFENCE",
"geometry": "MULTIPOLYGON(((120.64632 24.14296,120.6465 24.14295,120.64651 24.14271,120.64634 24.14271,120.64632 24.14296)))"
},
{
"attributes": {
"ID": "",
"GEOMETRY_ID": "7",
"NAME": "Talent Class"
},
"distance": 197.83,
"nearestLat": 24.14366,
"nearestLon": 120.64959,
"layerId": "MYGEOFENCE",
"geometry": "MULTIPOLYGON(((120.64959 24.14366,120.64978 24.14367,120.64983 24.14352,120.64964 24.14344,120.64959 24.14366)))"
},
{
"attributes": {
"ID": "",
"GEOMETRY_ID": "5",
"NAME": "Home"
},
"distance": 388.32,
"nearestLat": 24.14058,
"nearestLon": 120.64808,
"layerId": "MYGEOFENCE",
"geometry": "MULTIPOLYGON(((120.64808 24.14058,120.64871 24.14053,120.64871 24.14023,120.64807 24.1402,120.64808 24.14058)))"
},
{
"attributes": {
"ID": "",
"GEOMETRY_ID": "8",
"NAME": "Grandpa's House"
},
"distance": 487.62,
"nearestLat": 24.14271,
"nearestLon": 120.64312,
"layerId": "MYGEOFENCE",
"geometry": "MULTIPOLYGON(((120.64295 24.14273,120.64312 24.14271,120.6431 24.14247,120.64292 24.14249,120.64295 24.14273)))"
},
{
"attributes": {
"ID": "",
"GEOMETRY_ID": "3",
"NAME": "Carrefour"
},
"distance": 583.21,
"nearestLat": 24.14919,
"nearestLon": 120.6488,
"layerId": "MYGEOFENCE",
"geometry": "MULTIPOLYGON(((120.64879 24.14987,120.64961 24.14986,120.64966 24.14916,120.6488 24.14919,120.64879 24.14987)))"
},
{
"attributes": {
"ID": "",
"GEOMETRY_ID": "1",
"NAME": "Internet cafe"
},
"distance": 589.17,
"nearestLat": 24.14922,
"nearestLon": 120.64645,
"layerId": "MYGEOFENCE",
"geometry": "MULTIPOLYGON(((120.64611 24.14936,120.64646 24.14936,120.64645 24.14922,120.6461 24.14922,120.64611 24.14936)))"
},
{
"attributes": {
"ID": "",
"GEOMETRY_ID": "2",
"NAME": "Library"
},
"distance": 643.06,
"nearestLat": 24.14941,
"nearestLon": 120.64533,
"layerId": "MYGEOFENCE",
"geometry": "MULTIPOLYGON(((120.64531 24.14982,120.64533 24.14941,120.64493 24.14941,120.64493 24.14983,120.64531 24.14982)))"
}
],
"meta": [
{
"layerId": "MYGEOFENCE",
"lastUpdateTimeStamp": 1601883400017
}
],
"response_code": "200 OK"
}
```
If you remove the search range and leave only the latitude and longitude (for example, 24.14405,120.64769), the returned geometries will be an empty array, which means that the location is not within any geographic fence:
```json=
{
"geometries": [],
"meta": [
{
"layerId": "MYGEOFENCE",
"lastUpdateTimeStamp": 1601883400017
}
],
"response_code": "200 OK"
}
```
And if we provide a known longitude and latitude within a certain geographic fence (for example, 24.14551,120.64487), you will get a return similar to the following, you can notice that the distance is negative, and nearestLat and nearestLon are both 0.0, which means you search The location is within this geographic fence.
```json=
{
"geometries": [
{
"attributes": {
"ID": "",
"GEOMETRY_ID": "4",
"NAME": "Park"
},
"distance": -9.9999999E7,
"nearestLat": 0.0,
"nearestLon": 0.0,
"layerId": "MYGEOFENCE",
"geometry": "MULTIPOLYGON(((120.6439 24.14674,120.64654 24.1467,120.64651 24.1439,120.64328 24.14422,120.6439 24.14674)))"
}
],
"meta": [
{
"layerId": "MYGEOFENCE",
"lastUpdateTimeStamp": 1601883400017
}
],
"response_code": "200 OK"
}
```
So with HERE Geofencing, you can not only know whether the location you want to query is within the geographic fence, you can also know how far it is from the specific geographic fence. You can also try to make a geographic fence of points and lines by yourself. The method is the same, but the format is different.
#### Also talk: What is the difference with Google's Geofencing?
When most people talk about maps, the first thing they think of is Google Maps, and [Google also provides the function of Geofencing](https://developer.apple.com/documentation/corelocation/monitoring_the_user_s_proximity_to_geographic_regions) ([In fact, iOS also has similar functions] (https://developer.apple.com/documentation/corelocation/monitoring_the_user_s_proximity_to_geographic_regions)), but Google’s Geofencing is implemented in the Android SDK, which is actually very different from HERE as a network API service.

###### Picture source: https://developers.google.com/location-context/geofencing
* In terms of architecture, HERE’s Geofencing does not rely on a specific SDK, so it supports all purposes, devices or platforms that can send and receive HTTP Requests, so you can freely choose to use it on servers, mobile phones, vehicles, wearable devices, and objects. On networked devices; Google’s Geofencing only supports Android.
* In terms of flexibility, HERE Geofencing supports points, lines, and surfaces (arbitrary polygons); while Google’s Geofencing only supports circles (position plus range).
* Logically speaking, HERE's Geofencing is more like "querying" to get "results"; and Google's Geofencing is more like listening to "events" to get "notifications."
* In terms of privacy, HERE's Geofencing will not collect, share, and reuse the latitude and longitude of the user's query, and HERE will not know whether the latitude and longitude of the query represents a specific object, such as vehicles, personnel, etc., for HERE's services In other words, this is just a simple latitude and longitude.
* In terms of usage:
* The purpose of HERE Geofencing is more like a specific management center that monitors assets under its jurisdiction, such as logistics companies monitoring fleets and goods, company management monitoring employees' locations inside the factory, and so on.
* Google’s Geofencing is more like a user monitoring a specific target, for example, the recipient monitors whether the goods arrive at their location, or the factory staff monitors whether they have entered a specific area of the factory (but only a circle) .
As for how to use it in practice, it depends on your needs.