mrk2011
mrk2011

Reputation: 1

Implement request as JSON for google geolocation API

I'm trying to implement google geolocation API in order to get a location by providing the cell data. Would like to make a form where you put in the cell info and get its location.

Here's the page i'm following: https://developers.google.com/maps/documentation/geolocation/intro

I have MNC, MCC, CID, but I really can't understand how to set up a form in HTML that POST a request, with the body in JSON.

Can anyone post me an example please?

Thanks!!

Upvotes: 0

Views: 1319

Answers (1)

Ahmed chiboub
Ahmed chiboub

Reputation: 31

i wrote a python script that return the GPS location of a cell. it uses Google Geolocation API, if there is no result, it looks up the coordinates using OpenCellID API.

#!/bin/python
"""
Written by Atissonoun - Credits to MFC & HAC
***You need to initialize the script in order to fix the import and the dependency.
This is only a Beta version of the project***
This python file works as the engine for the project.
imports, coordinates, run......
    """

#Importing modules
import requests
#defining a Api_Keys

Google_API_KEY="Your google API Key goes here"
OpenCell_Api_Key ="Your OpenCellID API Key goes here"

def Google(MMC,MNC,LAC,ID,API_KEY=Google_API_KEY):
    url = "https://www.googleapis.com/geolocation/v1/geolocate?key={}".format(API_KEY)
    data={
    "radioType": "gsm",
    "cellTowers":[
        {
        "cellId": ID,
        "locationAreaCode": LAC,
        "mobileCountryCode": MMC,
        "mobileNetworkCode": MNC
        }
    ]
    }
    response = requests.post(url, json=data)
    if response.status_code == 200 :
        lat=response.json()[u'location'][u'lat']
        long = response.json()[u'location'][u'lng']
        d={'LAT':lat,'LONG':long}
        print('Located Cell: {}'.format(ID))
        return d
    else:
        print('Error: {}'.format(response.status_code))
        return None

def Opencell(MMC,MNC,LAC,ID,API_KEY=OpenCell_Api_Key):
    url = "https://us1.unwiredlabs.com/v2/process.php"
    data = {
        "token": API_KEY,
        "radio": "gsm",
        "mcc": MMC,
        "mnc": MNC,
        "cells": [{
            "lac": LAC,
            "cid": ID
        }]
    }
    response = requests.post(url, json=data)
    if response.status_code == 200:
        if response.json()[u'status']== 'error':
            print('Error: {}'.format(response.json()[u'message']))
            return None
        else:
            lat = response.json()[u'lat']
            long = response.json()[u'lon']
            d = {'LAT': lat, 'LONG': long}
            print('Located Cell: {}'.format(ID))
            return d
    else:
        print('Error: {}'.format(response.status_code))
        return None

Upvotes: 0

Related Questions