2009-10-21 19 views
13

Secondo questo campione:google maps, CellID alla posizione

http://www.codeproject.com/KB/mobile/DeepCast.aspx

E 'possibile richiedere una coordinata GPS (longitudine & latitudine) con range durante l'invio di informazioni CellID (MCC, MNC, towerid, ecc)

Qualcuno può dirmi il parametro effettivo per richiedere/inviare a questo indirizzo?

http://www.google.com/glm/mmap

potrebbe essere qualcosa di simile

http://www.google.com/glm/mmap?mcc=xxx&mnc=xxx&towerid=xxx 

E vorrei sapere quale risposta che otterremmo.

Ho osservato il sito Web di OpenCellid e forniscono alcune API carine per cominciare, ma voglio sapere che anche in google map (poiché hanno un database più completo).

OpenCellID API

+0

Non ne sono sicuro. Ma con Google Maps, sembra che ogni volta che il mio posto abbia un nuovo ID Cellulare, dopo alcune settimane o un mese, Google Maps aggiorna quell'ID Cell. –

risposta

8

Ecco ad esempio per il lavoro con

#!/usr/bin/python 

country = 'fr' 
#device = 'Sony_Ericsson-K750' 
device = "Nokia N95 8Gb" 
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' 
mmap_url = 'http://www.google.com/glm/mmap' 
geo_url = 'http://maps.google.com/maps/geo' 

from struct import pack, unpack 
from httplib import HTTP 
import urllib2 

def fetch_latlong_http(query): 
    http = HTTP('www.google.com', 80) 
    http.putrequest('POST', '/glm/mmap') 
    http.putheader('Content-Type', 'application/binary') 
    http.putheader('Content-Length', str(len(query))) 
    http.endheaders() 
    http.send(query) 
    code, msg, headers = http.getreply() 
    result = http.file.read() 
    return result 

def fetch_latlong_urllib(query): 
    headers = { 'User-Agent' : user_agent } 
    req = urllib2.Request(mmap_url, query, headers) 
    resp = urllib2.urlopen(req) 
    response = resp.read() 
    return response 

fetch_latlong = fetch_latlong_http 

def get_location_by_cell(cid, lac, mnc=0, mcc=0, country='fr'): 
    b_string = pack('>hqh2sh13sh5sh3sBiiihiiiiii', 
        21, 0, 
        len(country), country, 
        len(device), device, 
        len('1.3.1'), "1.3.1", 
        len('Web'), "Web", 
        27, 0, 0, 
        3, 0, cid, lac, 
        0, 0, 0, 0) 

    bytes = fetch_latlong(b_string) 
    (a, b,errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih",bytes) 
    latitude = latitude/1000000.0 
    longitude = longitude/1000000.0 

    return latitude, longitude 

def get_location_by_geo(latitude, longitude): 
    url = '%s?q=%s,%s&output=json&oe=utf8' % (geo_url, str(latitude), str(longitude)) 
    return urllib2.urlopen(url).read() 

if __name__ == '__main__': 
    print get_location_by_cell(20465, 495, 3, 262) 
    print get_location_by_cell(20442, 6015) 
    print get_location_by_cell(1085, 24040) 
    print get_location_by_geo(40.714224, -73.961452) 
    print get_location_by_geo(13.749113, 100.565327) 
+0

Ma quale è la necessità del parametro del dispositivo? nei siti web non è richiesto. Grazie – ransh

+0

Stavo cercando la funzione pack e mi dà errore per i dati (745522,22,874,405) e l'errore è struct.error: l'argomento per 's' deve essere un oggetto bytes. vedi se riesci a spiegare che cosa è esattamente l'imballaggio spec da google mmap. – Miten

+0

@Miten Dai un'occhiata qui https://developers.google.com/maps/documentation/geolocation/intro Script sopra i 7 anni –

8

È possibile utilizzare l'API di localizzazione di Google utilizzata da Firefox (esempio vedere a http://www.mozilla.com/en-US/firefox/geolocation/) che contiene l'URL www.google.com/loc/json/. In realtà questo è JSON basa webservice e un minimo Perl Esempio simile a questa:

use LWP; 

my $ua = LWP::UserAgent->new; 
$ua->agent("TestApp/0.1 "); 
$ua->env_proxy(); 

my $req = HTTP::Request->new(POST => 'https://www.google.com/loc/json'); 

$req->content_type('application/jsonrequest'); 
$req->content('{"cell_towers": [{"location_area_code": "8721", "mobile_network_code": "01", "cell_id": "7703", "mobile_country_code": "262"}], "version": "1.1.0", "request_address": "true"}'); 

# Pass request to the user agent and get a response back 
my $res = $ua->request($req); 

# Check the outcome of the response 
if ($res->is_success) { 
    print $res->content; 
} else { 
    print $res->status_line, "\n"; 
    return undef; 
} 

Si prega di tenere presente che Google non ha aperto ufficialmente questa API per altri usi ...

+0

puoi dare qualche esempio di codice JS? – Dels

+1

grazie drago di Tane, ho scritto la versione di Ruby qui http://bintangjatuh.com/2011/01/27/ruby-cellid-to-location.html – gozali

+2

Da qualche mese questo non funziona più.Non è chiaro per me se ora devi utilizzare una chiave API nella richiesta o se la funzionalità è stata rimossa dall'API. – Lairsdragon

4

Base sul GeolocationAPI, qui ci sono alcune parti del mio codice:

import java.io.IOException; 
import java.io.StringWriter; 
import java.net.HttpURLConnection; 
import java.net.URL; 

//http://code.google.com/p/google-gson/ 
import com.google.gson.stream.JsonWriter; 

... 

/** 
* Requests latitude and longitude from Google. 
* 
* @param gsmParams 
*   {@link GsmParams} 
* @return an {@link HttpURLConnection} containing connection to Google 
*   lat-long data. 
* @throws IOException 
*/ 
public HttpURLConnection requestLatlongFromGoogle(GsmParams gsmParams) 
     throws IOException { 
    // prepare parameters for POST method 
    StringWriter sw = new StringWriter(); 
    JsonWriter jw = new JsonWriter(sw); 
    try { 
     jw.beginObject(); 

     jw.name("host").value("localhost"); 
     jw.name("version").value("1.1.0"); 
     jw.name("request_address").value(true); 

     jw.name("cell_towers"); 

     jw.beginArray().beginObject(); 

     jw.name("cell_id").value(gsmParams.getCid()); 
     jw.name("location_area_code").value(gsmParams.getLac()); 
     jw.name("mobile_network_code").value(gsmParams.getMnc()); 
     jw.name("mobile_country_code").value(gsmParams.getMcc()); 

     jw.endObject().endArray().endObject(); 
    } finally { 
     try { 
      jw.close(); 
     } catch (IOException ioe) { 
     } 
     try { 
      sw.close(); 
     } catch (IOException ioe) { 
     } 
    } 
    final String JsonParams = sw.toString(); 
    final String GoogleLocJsonUrl = "http://www.google.com/loc/json"; 

    // post request 
    URL url = null; 
    HttpURLConnection conn = null; 
    url = new URL(GoogleLocJsonUrl); 
    conn = (HttpURLConnection) url.openConnection(); 
    conn.setConnectTimeout((int) 30e3); 
    conn.setReadTimeout((int) 30e3); 

    conn.setDoOutput(true); 
    conn.setRequestMethod("POST"); 
    conn.getOutputStream().write(JsonParams.getBytes()); 
    conn.getOutputStream().flush(); 
    conn.getOutputStream().close(); 
    int resCode = conn.getResponseCode(); 
    if (resCode == Http_BadRequest || resCode != Http_Ok) { 
     throw new IOException(String.format(
       "Response code from Google: %,d", resCode)); 
    } 
    return conn; 
} 

L'oggetto GsmParams è solo un bean Java contenente parametri GSM MCC, MNC, LAC, CID. Penso che tu possa creare facilmente una stessa classe.

Dopo aver ottenuto la connessione, è possibile chiamare conn.getInputStream() e ottenere risultati da Google Maps. Quindi utilizzare JsonReader per analizzare i dati ...

+1

Poiché Google Gears è stato chiuso da marzo 2011, è giunto il momento di dire addio all'API di geolocalizzazione che ha attivato Google Gears. L'API di geolocalizzazione di Google Gears smetterà di rispondere alle richieste il 17 novembre 2012. –

Problemi correlati