# (15) - HERE Positioning - Network Positioning ![](https://i.imgur.com/Vcb3qEn.jpg) There is a very philosophical motto that goes like this: ![](https://i.imgur.com/E4ewK3F.png) ###### Picture source: https://www.azquotes.com/quote/875522 "Positioning" and "direction" are inseparable. Give you a string of latitude and longitude without a map, it doesn't make much sense to you. Having a map but if you don't know where you are now, it doesn't make much sense as well. We talked about "map" related applications, such as searching for locations, planning routes, etc., all for the purpose of "destination", but before you find the destination, how do you know your current location? Generally speaking, navigation devices, mobile phones, and even smart watches often have GNSS (Global Navigation Satellite System) receivers, whether it is GPS of the United States, GLONASS of Russia, Galileo of the EU, BeiDou of China, QZSS of Japan uses the distance between the satellite and the receiver to calculate the position. ![](https://i.imgur.com/nbh55Dv.gif) ###### Source: https://en.wikipedia.org/wiki/Global_Positioning_System However, in many cases, GNSS is not really useful. For example, when the satellite signal is obscured, the number of visible satellites decreases, which will cause the satellite positioning error to become large, even to the point where it is not referenceable. For example, the Civic Avenue in Taipei City is surrounded by tall buildings with a viaduct above it, and the GNSS positioning can easily drift. Or when indoors or underground, because there is no GNSS signal, GNSS positioning cannot find the location. ![](https://i.imgur.com/GzrzwDa.jpg) ###### Source: https://goo.gl/maps/XipcG6vWt7erkrWF9 Therefore, in each of our mobile phones, in addition to the GNSS receiver, there will also be network positioning technology. The so-called network positioning refers to the base station of the mobile phone, whether it is 2G/3G/4G/5G/WCDMA, etc., or use WiFi wireless network hotspots, or even Bluetooth or LoRa, the principle is similar. They measure the point where the signal is sent and the point where the signal is received for positioning, so it can receive more signal sources and positioning The accuracy will also improve. In addition to providing positioning when there is no GNSS signal, the built-in network positioning of the mobile phone has additional benefits: because GNSS positioning needs to receive ephemeris, so that the position of each satellite can be known, and then the GNSS satellites can be known from the GNSS signal. The distance to the receiver and then the approximate position is calculated. This process takes time, ranging from tens of seconds to several minutes. During this time, there is no way to obtain the position solely by GNSS, but perhaps in the past, everyone was used to it. Wait for the GPS to turn on cold and get the positioning. In the current situation of one mobile phone, we often need to know the current position immediately. At this time, the network positioning is very important, because the network positioning is to collect the source of the wireless signal that the mobile phone can receive. For example, the base station of the mobile network and the hotspot of the wireless network, and then calculate the location through a specific network service, so that the first time to fix (FTTF) time can be effectively shortened to a few seconds. ![](https://i.imgur.com/fu02e6f.jpg) However, the accuracy obtained by different positioning methods is also very different. Generally speaking, when using mobile network base stations, the error range obtained may range from hundreds of meters to several kilometers, so it is usually only used to obtain approximate Location; however, if it is a WiFi wireless base station, it can reach an error range of tens of meters to a few meters; but if using Bluetooth, it can reach an error range of several meters to tens of centimeters, which is almost A few steps away. In traditional GPS positioning, in open areas with good reception, the error range can be as low as a few meters. To obtain higher accuracy results, more complex (higher cost) technologies must be used. Among the technologies mentioned above, Bluetooth has the highest positioning accuracy, but it has inherent defects: it must have a Bluetooth signal, and in order to have a Bluetooth signal, a lot of Bluetooth beacons (Beacon) must be installed first. , This is also a condition that ordinary places will not have, so the most mainstream at present is still mobile network and WiFi wireless network. ### HERE's network positioning service The network positioning service currently provided by HERE also supports the three technologies just mentioned: 1. Mobile network: API and HERE SDK (Android) 2. WiFi wireless network: API and HERE SDK (Android) 3. Bluetooth: HERE SDK (iOS/Android) If you use the network API for positioning, you can use the mobile network and WiFi; if you use the HERE SDK (Android), you can use all three; if you use the HERE SDK (iOS), you can only use Bluetooth positioning. It is related to the different permissions opened by Android/iOS. What we are going to introduce this time is the positioning technology using web API. ※ HERE Positioning official website: https://developer.here.com/documentation/positioning/ HERE Positioning API is a technology that uses API for positioning. We can use this API to upload the list of mobile network base stations and WiFi wireless network hotspots we have obtained, and HERE will calculate an approximate location and send it back to us. If you use a general mobile phone APP, you usually don’t need to worry about such problems, but if you are developing an IoT device today, for example, use a Raspberry Pi development board plus a number of sensors to measure temperature, humidity, wind direction, etc. Weather information, but weather information must be combined with geographic location. HERE network positioning is a very useful service, because most of the current development boards support WiFi wireless networks. You can scan nearby WiFi by writing a simple program. Hotspots, and upload the information to the HERE network positioning API to get the location, so you can save the cost of the GPS chip and also save a lot of development time. ![](https://i.imgur.com/NhH2qUT.jpg) ###### Picture source: https://en.wikipedia.org/wiki/Raspberry_Pi ### Realize WiFi wireless network positioning You can test with a Windows computer in your hand. If your computer can receive WiFi wireless network, you can perform the following steps. Please open the command line mode first, press "Windows + R" to open the execution dialog, then type "`cmd`" in the text box, and then press "Enter" or "OK" to open the command line mode. ![](https://i.imgur.com/2Z5jytT.png) Then please enter the following command, and then press "Enter": ``` netsh wlan show network bssid ``` After pressing Enter, you will see a string of text running past. These are the list of WiFi wireless networks scanned by your computer. ![](https://i.imgur.com/dfFoEcN.gif) You can output this list of lists as text files, which will be easier to read. For example, we can use the following command: ``` netsh wlan show network bssid> wifi_list.txt ``` After pressing "Enter", no information will be displayed on the screen. Instead, it will be output to a text file called wifi_list.txt. Let's open this text file to try, for example, the first SSID I scanned It is called WeWork, and there are several BSSIDs below, that is, the WAN MAC address of each WiFi wireless network base station. In addition, the signal strength, channel, specification (802.11ac), and speed of each base station are fully displayed. ``` Interface name : Wi-Fi There are 34 networks currently visible. SSID 1 : WeWork Network type : Infrastructure Authentication : WPA2-Personal Encryption : CCMP BSSID 1 : 1c:3a:60:14:89:3c Signal : 53% Radio type : 802.11ac Channel : 149 Basic rates (Mbps) : 24 Other rates (Mbps) : 36 48 54 BSSID 2 : 1c:3a:60:14:83:9c Signal : 81% Radio type : 802.11ac Channel : 161 Basic rates (Mbps) : 24 Other rates (Mbps) : 36 48 54 BSSID 3 : 1c:3a:60:14:34:3c Signal : 60% Radio type : 802.11ac Channel : 149 Basic rates (Mbps) : 24 Other rates (Mbps) : 36 48 54 BSSID 4 : 1c:3a:60:14:48:9c Signal : 62% Radio type : 802.11ac Channel : 149 Basic rates (Mbps) : 24 Other rates (Mbps) : 36 48 54 BSSID 5 : 1c:3a:60:14:34:dc Signal : 78% Radio type : 802.11ac Channel : 44 Basic rates (Mbps) : 24 Other rates (Mbps) : 36 48 54 ``` The number of WiFi wireless base stations scanned varies from location to location. If you are in a deep mountain, you may not find one; but if you are in an office building, you may scan dozens or even hundreds! In the example I scanned, a total of 111 base stations were found. In addition, the sensitivity of the receiver of the device or the degree of electromagnetic interference in the environment will affect the number of wireless base stations found. Therefore, even in the same location, the number of mobile phones, tablets, and laptops found may be slightly different. Although the number of base stations is better, the HERE network positioning API is basically as long as there are more than three wireless base stations to find the location. Then we will use this information for network positioning, but this part will be a bit like manual operation. The JSON format that HERE web positioning API needs to upload is: ``` { "wlan": [{ "mac": "1c:3a:60:54:6e:3c" }, { "mac": "1c:3a:60:14:83:9c" }, { "mac": "1c:3a:60:14:34:3c" }, { "mac": "1c:3a:60:14:48:9c" }, { "mac": "1c:3a:60:14:34:dc" }] } ``` Please change the MAC address after "mac in the above string of JSON string to the result of your scan, copy and paste from wifi_list.txt, we will take five of them first, of course you can also test more Whether the results obtained by a little bit or a little bit are different. Then we open Postman, add a POST request, and fill in the following information: 1. Website: `https://pos.ls.hereapi.com/positioning/v1/locate` 2. apiKey: Your HERE API KEY. ![](https://i.imgur.com/wc9exye.png) Then we switch to the Headers tab and add a Key/Value: Key: `Content-Type` Value: `application/json` ![](https://i.imgur.com/Q5AgSad.png) Then we switch to the Body tab, select "raw", and paste the edited JSON string above in the text box below. ![](https://i.imgur.com/cwqqR4R.png) Then press "Send" to send the request. If the wireless base station information you sent can get the location, you will get a return result similar to the following, which includes lat (latitude), lng (longitude), accuracy (accuracy) ). ``` { "location": { "lat": 25.0358719, "lng": 121.5684867, "accuracy": 118 } } ``` Based on the above result, the error range is 118 meters, which is not a very good result. What if we increase the number of wireless base stations? Will the result be better? According to actual measurement, increasing the number of wireless base stations has indeed reduced the error range from 118 meters to 57 meters. ![](https://i.imgur.com/sq8Ceey.png) ### Use HERE Map Image API to draw positioning results In fact, the steps just now are a bit manual, which is really too slow. Generally speaking, we can scan in a program to carry out the process just now. The following small program written in Python 3.x supports Windows and Mac OS. It will go through the steps just now to obtain the results of WiFi wireless network scanning first, and compile the obtained results into the format required for HERE network positioning, and upload them. As a result, use the HERE Map Image API to draw the results (latitude and longitude, margin of error) into a map image and download it. You only need to replace the content of the **api_key** variable with your own HERE API KEY (line number 32), and then run it directly. ```python= import json import platform import plistlib import re import subprocess import time import requests def rev_geocoder(lat, lon, radius): hls_rev_geocoder_url = 'https://reverse.geocoder.ls.hereapi.com/6.2/reversegeocode.json?prox={},{},{}&mode=retrieveAddress&maxresults=1&gen=9&apiKey={}'.format(lat, lon, radius, api_key) hls_rev_geocode_r = requests.get(hls_rev_geocoder_url) location = json.loads(hls_rev_geocode_r.text)['Response']['View'][0]['Result'][0]['Location'] address = location['Address']['Label'] display_position = location['DisplayPosition'] print('Address: {}'.format(address)) return (display_position['Latitude'], display_position['Longitude'], address) def mia_cicular_picture(lat, lon, radius, label): mia_url = 'https://image.maps.ls.hereapi.com/mia/1.6/mapview?c={},{}&u={}&w=1440&h=900&ml=cht' \ '&ppi=72&apiKey={}&t=3&tx={},{};{}&txs=30'.format(lat, lon, radius, api_key, lat - 0.00005, lon, label) image_data = requests.get(mia_url).content timestamp = time.strftime('%Y_%m_%d_%H_%M_%S', time.localtime(time.time())) image_file_name = 'here_network_positioning_result_{}.jpg'.format(timestamp) positioning_log.write('{}\t{}\t{}\t{}\n'.format(timestamp, wifi_scan_result, network_positioning_r.text, label)) with open(image_file_name, 'wb') as handler: handler.write(image_data) api_key = '' # YOUR HERE API KEY positioning_headers = {'Content-Type': 'application/json'} positioning_url = 'https://pos.ls.hereapi.com/positioning/v1/locate?apiKey={}'.format(api_key) if platform.system() == 'Windows': results = subprocess.check_output(["netsh", "wlan", "show", "network", "bssid"]) results = results.decode("ascii", errors='ignore') # needed in python 3 results_list = results.replace('\r', '').replace(' ', '').split('\n\n') i = 0 parsed_result_list = [] for results in results_list: results = results.split('\n') for result in results: if result.startswith('BSSID') or result.startswith(' Signal'): parsed_result_list.append(result.split(' : ')[1].strip()) else: continue mac_list = set() while i < len(parsed_result_list): if i % 2 == 1: signal_percentage = int(parsed_result_list[i].replace('%', '')) mac_list.add('{"mac": "' + parsed_result_list[i - 1] + '", "powrx": ' + str(int((signal_percentage / 2) - 100)) + '}') i += 1 elif platform.system() == 'Darwin': command = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s -x' # the shell command process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=True) output = process.communicate() hotspots_plist = plistlib.loads(str.encode(output[0].decode('utf-8', errors='ignore')), fmt=plistlib.FMT_XML) mac_list = set() for hotspot in hotspots_plist: ssid_str = hotspot['SSID_STR'] bssid = hotspot['BSSID'] rssi = hotspot['RSSI'] if not re.findall('\w\w:\w\w:\w\w:\w\w:\w\w:\w\w', hotspot['BSSID']): bssid_arr = bssid.split(':') bssid_modified_arr = [] for bssid_elem in bssid_arr: if len(bssid_elem) == 1: bssid_elem = '0{}'.format(bssid_elem) bssid_modified_arr.append(bssid_elem) bssid = ':'.join(bssid_modified_arr) mac_list.add('{\"mac\":\"' + bssid + '\",\"powrx\":' + str(rssi) + '}') # BSSID + Powrx wifi_scan_result = '{"wlan":[' + ','.join(i for i in mac_list) + ']}' print('Wifi hotspots:\n' + wifi_scan_result) network_positioning_r = requests.post(url=positioning_url, data=wifi_scan_result, headers=positioning_headers) if network_positioning_r.status_code == 200: json_result = json.loads(network_positioning_r.text) print('result:\n' + str(json_result)) lat = json_result['location']['lat'] lon = json_result['location']['lng'] positioning_log = open('positioning_result_log.txt', mode='a', encoding='utf-8') accuracy = json_result['location']['accuracy'] address_lat, address_lon, address_label = rev_geocoder(lat, lon, accuracy) mia_cicular_picture(lat, lon, accuracy, address_label) else: print(network_positioning_r.text) ``` Let's take a look at the execution result. The terminal will print the written JSON string and also print the positioning result (`{'location': {'lat': 25.0360984,'lng': 121.5686241,'accuracy': 29}}`). ![](https://i.imgur.com/WuRozhl.png) The downloaded picture (pos_result.jpg) will look like this, the green dot above represents the latitude and longitude position of the positioning result, and the green circle is the error range. ![](https://i.imgur.com/T1oiDTf.jpg) Simply write a small program to obtain the location through the HERE network positioning service, which is a very convenient and low-cost method for IoT development. But if your need is to use mobile phone wireless base station information to perform network positioning, you can also go to [Official website](https://developer.here.com/documentation/positioning/) to refer to technical documents for related implementation methods .