PubNub Integration

Mapbox: Geolocation

IoT Device Control

IoT Device Control

Geolocation

Geolocation

Last Updated:

2016/08/03

Author:

Ian Jennings

Plot location on a realtime, live-updating map

Mapbox's IP-to-Geolocation API plots a user's location on a map based on their IP address, without having to ask the client for the geolocation data.

The Mapbox geolocation block allows you to plot user or device location, and move that marker based on changes in location, in realtime. In streaming coordinates, any number of subscribers can watch changes in geolocation on the live-updating map.

Walkthrough

Whenever a client publishes to the PubNub network, it's IP is added as a meta value to the request packet and is available in a block under request.client.ip. Then the IP's location is estimated using the free ip-api.com API. It's super simple, all we need to do is make a request to http://ip-api.com/json/[ip]

The ip-api.com service will respond with an estimated geolocation, city, state, country, as well as information about the internet service provider (ISP). Here's an example of a response from the service:

{
    "as":"AS36351 SoftLayer Technologies Inc.",
    "city":"San Francisco (Haight-Ashbury)",
    "country":"United States",
    "countryCode":"US",
    "isp":"SoftLayer Technologies",
    "lat":37.7714,
    "lon":-122.447,
    "org":"SoftLayer Technologies",
    "query":"169.53.82.164",
    "region":"",
    "regionName":"California",
    "status":"success",
    "timezone":"America/Los_Angeles",
    "zip":""
}

Now if you subscribe to eon-map-geolocation-output using the same keys supplied into the block, you'll see the EON formatted output ready to be plotted.

Input

This block can take any payload as input, the contents do not matter as the IP is taken from the message's meta properties.

{ 
    "text" : "Hi" 
}

Output

The block publishes information about the client's location onto an output channel.

{ 
    '182.64.236.153': { 
        latlng: [ 28.4667, 77.0333 ],
		data: {
            as: 'AS24560 Bharti Airtel Ltd., Telemedia Services',
			city: 'Gurgaon',
			country: 'India',
			countryCode: 'IN',
			isp: 'Airtel Broadband',
			lat: 28.4667,
			lon: 77.0333,
			org: 'Airtel Broadband',
			query: '182.64.236.153',
			region: 'HR',
			regionName: 'Haryana',
			status: 'success',
			timezone: 'Asia/Kolkata',
			zip: '122001' 
		} 
    } 
}

Feedback & Support

Need help? Contact PubNub Support.


Talk to an expert

Contact Sales