2016-12-06 1 views
0

셀 데이터를 제공하여 위치를 얻으려면 google geolocation API를 구현하려고합니다. 셀 정보를 입력하고 위치를 가져 오는 양식을 만들고 싶습니다. 여기 google geolocation API 용 JSON으로 요청 구현

는 내가 소식 페이지의 : 나는 MNC, MCC, CID가 https://developers.google.com/maps/documentation/geolocation/intro

,하지만 난 정말 JSON의 몸, 요청을 POST HTML에서 양식을 설정하는 방법을 이해할 수 없다 .

아무에게도 나에게 예제를 게시 할 수 있습니까?

감사합니다.

+1

가능한 복제 [URL로 JSON 통화를하는 방법?] (http://stackoverflow.com/questions/2499567/how-to-make-a-json-call-to-a- URL) – Blackbam

+0

그런 다음 Google에서 요청하는 방법은 무엇입니까? – Blackbam

답변

0

나는 세포의 GPS 위치를 반환하는 python 스크립트를 작성했습니다. Google Geolocation API를 사용합니다. 결과가 없으면 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