External Exam Download Resources Web Applications Games Recycle Bin

Geolocation API

To install Mapbox:

pip install mapbox



Sign up for free public key from Mapbox.

JavaScript examples

Mapbox Python SDK

download_still_image.py

from mapbox import Static

lon = 153.0251
lat = -27.4698
zoom = 15
rotation = 0
tilt = 0
token = "pk.eyJ1IjoibWl0aTIiLCJhIjoiY2puNDdzdG5sMDZsaDNrcDZ4ZHRuN2E5cyJ9.dd0qe7G2vnsB-xTWIShziQ"
service = Static(access_token=token)

person = {
    'type': 'Feature',
    'properties': {'name': 'John Citizen'},
    'geometry': {
        'type': 'Point',
        'coordinates': [lon, lat]
    }
}

#mapbox types: satellite, outdoors, streets-satellite, streets-basic:
response = service.image('mapbox.streets-satellite',features=[person],lon=lon, lat=lat, z=zoom)
with open('tmpmap.png', 'wb') as output:
    output.write(response.content)

JSmap.py

from flask import Flask
from flask import request
from flask import render_template_string

app = Flask(__name__)

log = {}
tag_counter = 0

client = '''
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.49.0/mapbox-gl.js'></script>
<div id="map" style="height: 300px; width: 300px;"></div>
<script>mapboxgl.accessToken = "pk.eyJ1IjoibWl0aTIiLCJhIjoiY2puNDdzdG5sMDZsaDNrcDZ4ZHRuN2E5cyJ9.dd0qe7G2vnsB-xTWIShziQ";
map = new mapboxgl.Map({ container: 'map', style: 'mapbox://styles/mapbox/streets-v9' });
function success(pos) {document.getElementById("lat").value=pos.coords.latitude;document.getElementById("lon").value=pos.coords.longitude;
map.flyTo({center: [pos.coords.longitude, pos.coords.latitude], zoom: 15});}
function error(err){document.getElementById("map").innerHTML="NO GPS SIGNAL, REFRESHING PAGE IN 5 SECONDS";
setTimeout(function(){window.location.reload(1);}, 5000);}
navigator.geolocation.watchPosition(success,error,{enableHighAccuracy: true,timeout: 3000,maximumAge: 0});</script>
<form action="/log" method="POST">
  lat: <input type="text" name="lat" id="lat" value="0" readonly><br>
  lon: <input type="text" name="lon" id="lon" value="0" readonly><br>
  tag: <input type="text" name="tag" id="tag" value="test tag"><br>
  <input type="submit" value="log gps">
</form><br>{% if log is defined %}{% for key, value in log.items() %}Tag: {{key}}<br>LatLon: {{value}}<br>{% endfor %}{% endif %}
'''

@app.route("/log", methods=['GET', 'POST'])
def logTag():
    global log
    global tag_counter
    tag_counter += 1
    lat = request.form["lat"]
    lon = request.form["lon"]
    tag = str(tag_counter)+"_"+request.form["tag"]
    log[tag] = (lat, lon)
    return render_template_string(client, log=log)

@app.route("/")
def start():
    return render_template_string(client)

app.run()