java json e geolocation api google maps

Linguaggi di programmazione: php, perl, python, C, bash e tutti gli altri.
Chiusa
Chry1991
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 461
Iscrizione: giovedì 23 maggio 2013, 20:20
Desktop: Linux Mint 16.04
Distribuzione: Linux Mint 16.04 Xubuntu 16.04
Sesso: Maschile

java json e geolocation api google maps

Messaggio da Chry1991 »

Salve amici

sto seguendo questa guida:
https://developers.google.com/maps/docu ... o#overview

perchè avrei bisogno di tracciare gli hotspot tramite macaddress in java.
qui nella guida dice che l'api come richiesta vuole un argomento di questo tipo:

Codice: Seleziona tutto

{
  "macAddress": "01:23:45:67:89:AB",
  "signalStrength": -65,
  "age": 0,
  "channel": 11,
  "signalToNoiseRatio": 40
}
io mi chiedo come posso fare cio??

sto seguendo questo codice che però fa una cosa similare con quest api
https://developers.google.com/maps/docu ... ding/intro

il mio codice

Codice: Seleziona tutto

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonReader {



  public static void main(String[] args) throws ParseException  {


            InputStream inputStream = null;
            String json = "";

            try {           
                HttpClient client = new DefaultHttpClient();  
                HttpPost post = new HttpPost("https://maps.googleapis.com/maps/api/geocode/json?italy");
                HttpResponse response = client.execute(post);
                HttpEntity entity = response.getEntity();
                inputStream = entity.getContent();
            } catch(Exception e) {
            }

            try {           
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"),8);
                StringBuilder sbuild = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sbuild.append(line);
                }
                inputStream.close();
                json = sbuild.toString();               
            } catch(Exception e) {
            }


            //now parse
            JSONParser parser = new JSONParser();
            Object obj = parser.parse(json);
            JSONObject jb = (JSONObject) obj;

            //now read
            JSONArray jsonObject1 = (JSONArray) jb.get("results");
            JSONObject jsonObject2 = (JSONObject)jsonObject1.get(0);
            JSONObject jsonObject3 = (JSONObject)jsonObject2.get("geometry");
            JSONObject location = (JSONObject) jsonObject3.get("location");

            System.out.println( "Lat = "+location.get("lat"));
            System.out.println( "Lng = "+location.get("lng"));


        }
    }
come faccio per usarlo riaddattandolo alla mia ricerca e alla mia api??
Ultima modifica di Chry1991 il venerdì 20 maggio 2016, 23:55, modificato 2 volte in totale.
Chry1991
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 461
Iscrizione: giovedì 23 maggio 2013, 20:20
Desktop: Linux Mint 16.04
Distribuzione: Linux Mint 16.04 Xubuntu 16.04
Sesso: Maschile

Re: java json e geolocation api google maps

Messaggio da Chry1991 »

nessuno in grado di darmi un suggerimento?
Chry1991
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 461
Iscrizione: giovedì 23 maggio 2013, 20:20
Desktop: Linux Mint 16.04
Distribuzione: Linux Mint 16.04 Xubuntu 16.04
Sesso: Maschile

Re: java json e geolocation api google maps

Messaggio da Chry1991 »

ho risolto chiudo.
Avatar utente
Zoff
Moderatore Globale
Moderatore Globale
Messaggi: 33338
Iscrizione: mercoledì 10 ottobre 2007, 22:36

Re: java json e geolocation api google maps

Messaggio da Zoff »

Prima di aprire una discussione leggi le Guide, poi vedi se c'è un HowTo nel Wiki e fai una ricerca nel Forum!
Applica semplicemente il [Risolto]! Prova: http://forum.ubuntu-it.org/viewtopic.php?f=70&t=548821
Vuoi qualcosa di piu' dal forum? Prova i miei script: http://forum.ubuntu-it.org/viewtopic.php?f=70&t=597066
Chry1991
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 461
Iscrizione: giovedì 23 maggio 2013, 20:20
Desktop: Linux Mint 16.04
Distribuzione: Linux Mint 16.04 Xubuntu 16.04
Sesso: Maschile

Re: java json e geolocation api google maps

Messaggio da Chry1991 »


ero convinto di aver risolto...
poi ho fatto testing stasera mettendo il mio macaddress del mio router e ha tirato fuori coordinate che non centrano con la mia posizione.
Ecco qui il codice ma non capisco bene come usare sto JSON benedetto:

Codice: Seleziona tutto

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.*;

public class JSONProva {
	public static void main(String[]args) throws IOException{
		String dato = "";
		String line = "";
			post(dato);
			get(line);
	}
	
	public static String post(String dato) throws ClientProtocolException, IOException{
		BufferedReader rd;
		OutputStreamWriter wr;
		String line = "";
		HttpPost request = new HttpPost("https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyAZgdF6OdbxfZmKZ8nJmcrIT_clJq3MlBg");
	 
	    StringBuilder sb=new StringBuilder();
		
		try{
			
			JSONObject obj = new JSONObject();
			    obj.put("channel", 8);
				obj.put("macAddress", "01:23:45:67:89:AB");
				obj.put( "signalStrength", 8 );
				
				
				JSONObject obj2 = new JSONObject();
			   
				obj2.put("macAddress", "01:23:45:67:89:AB");
				obj2.put( "signalStrength", 4 );
				
				JSONArray ja = new JSONArray();
				ja.add(obj);
				ja.add(obj2);
				
				
				JSONObject main = new JSONObject();
				main.put("wifiAccessPoints", ja);
				System.out.println(main);
				

			    StringEntity entity = new StringEntity(main.toString());
			                         entity.setContentType("application/json;charset=UTF-8");
			                        
			                         request.setHeader("Accept", "application/json");
			                         request.setEntity(entity); 
			                         System.out.println("Entity:" + entity.toString());
			                         HttpResponse response =null;
			                         DefaultHttpClient httpClient = new DefaultHttpClient();

			                      
			                         try{
			                        	 System.out.println("Request:" + request);
			                         response = httpClient.execute(request); 
			                         System.out.println("response:" + response);
			                         }
			                         catch(SocketException se)
			                         {
			                          
			                             throw se;
			                         }




			    InputStream in = response.getEntity().getContent();
			    System.out.println("inputstream:" + in.toString() );
			    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			     line = null;
			    while((line = reader.readLine()) != null){
			        sb.append(line);

			    }
			    System.out.println(sb.toString());
			    return sb.toString();
			  
				
		} catch (Exception ex)
		{
			
		}
		finally {
			
			 }
		return dato;

	}
	
	
	public static String get(String risposta) throws IOException{
		BufferedReader rd;
		OutputStreamWriter wr;
		String line = "";
		try{
			URL url = new URL("https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyAZgdF6OdbxfZmKZ8nJmcrIT_clJq3MlBg");
			URLConnection conn = url.openConnection();
			conn.setDoOutput(true);
			wr = new OutputStreamWriter(conn.getOutputStream());
			wr.flush();
			
			rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			
			while((line = rd.readLine()) != null){
				System.out.println(line);
			}
			
			
		}catch (Exception ex){
			
		} finally {
			
		}
		return line;
	}
}
ti posto i risultati della console:

Codice: Seleziona tutto

{"wifiAccessPoints":[{"signalStrength":8,"macAddress":"C0:FF:D4:9B:F5:92","channel":8},{"signalStrength":4,"macAddress":"5C:33:8E:09:31:55"}]}
Entity:[Content-Type: application/json;charset=UTF-8,Content-Length: 142,Chunked: false]
Request:POST https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyAZgdF6OdbxfZmKZ8nJmcrIT_clJq3MlBg HTTP/1.1
response:HTTP/1.1 200 OK [Cache-Control: no-cache, no-store, max-age=0, must-revalidate, Pragma: no-cache, Expires: Mon, 01 Jan 1990 00:00:00 GMT, Date: Fri, 20 May 2016 22:46:38 GMT, Vary: X-Origin, Content-Type: application/json; charset=UTF-8, X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, X-XSS-Protection: 1; mode=block, Server: GSE, Alternate-Protocol: 443:quic, Alt-Svc: quic=":443"; ma=2592000; v="34,33,32,31,30,29,28,27,26,25", Accept-Ranges: none, Vary: Origin,Accept-Encoding, Transfer-Encoding: chunked] org.apache.http.conn.BasicManagedEntity@1af961
inputstream:org.apache.http.conn.EofSensorInputStream@ea6f21
{ "location": {  "lat": 45.9349056,  "lng": 8.8089323 }, "accuracy": 52.0}

----queste coordinate piu o meno si avvicinano tracciando gli hotspot----


{
queste che sono resituite dalla get credo manco lontamente si posizionano in centro paese circa

Codice: Seleziona tutto

{
 "location": {
  "lat": 45.9366118,
  "lng": 8.8076295
 },
 "accuracy": 784.0
}
Avatar utente
SuperStep
Entusiasta Emergente
Entusiasta Emergente
Messaggi: 2037
Iscrizione: lunedì 19 dicembre 2011, 16:26
Desktop: Unity
Distribuzione: Ubuntu 16.04 LTS x86_64
Sesso: Maschile
Località: Somma Vesuviana (NA)

Re: java json e geolocation api google maps

Messaggio da SuperStep »

Guarda credo che proprio il servizio non sia preciso.

Se vuoi avere una affidabilita' di localizzazione maggiore l'unica e' utilizzare il GPS (https://developer.android.com/guide/top ... egies.html). Altrimenti questa e' una mappatura che google fa dei "sistemi" collegati alla rete.

Ad esempio, provando direttamente (https://developers.google.com/maps/docu ... ngRequests) ottengo una distanza di circa 20-25km in linea d'aria dalla mia posizione reale.
ubuntu 16.04 LTS 64-bit - Memoria: 31,3 Gib - Processore: Intel Core i7-5960X CPU @ 3.00 GHz × 16 - Grafica: AMD Radeon HD 7800 Series - Disco: SSD 256 GB x 4 (RAID 01)
Chry1991
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 461
Iscrizione: giovedì 23 maggio 2013, 20:20
Desktop: Linux Mint 16.04
Distribuzione: Linux Mint 16.04 Xubuntu 16.04
Sesso: Maschile

Re: java json e geolocation api google maps

Messaggio da Chry1991 »

SuperStep [url=http://forum.ubuntu-it.org/viewtopic.php?p=4884038#p4884038][img]http://forum.ubuntu-it.org/images/icons/icona-cita.gif[/img][/url] ha scritto:Guarda credo che proprio il servizio non sia preciso.

Se vuoi avere una affidabilita' di localizzazione maggiore l'unica e' utilizzare il GPS (https://developer.android.com/guide/top ... egies.html). Altrimenti questa e' una mappatura che google fa dei "sistemi" collegati alla rete.

Ad esempio, provando direttamente (https://developers.google.com/maps/docu ... ngRequests) ottengo una distanza di circa 20-25km in linea d'aria dalla mia posizione reale.
si ma col GPS posso prendere solo la posizione di dove sono io ora e non di dove si trova l'hotspot.
Chry1991
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 461
Iscrizione: giovedì 23 maggio 2013, 20:20
Desktop: Linux Mint 16.04
Distribuzione: Linux Mint 16.04 Xubuntu 16.04
Sesso: Maschile

Re: java json e geolocation api google maps

Messaggio da Chry1991 »

up
Avatar utente
Zoff
Moderatore Globale
Moderatore Globale
Messaggi: 33338
Iscrizione: mercoledì 10 ottobre 2007, 22:36

Re: java json e geolocation api google maps

Messaggio da Zoff »

Prima di aprire una discussione leggi le Guide, poi vedi se c'è un HowTo nel Wiki e fai una ricerca nel Forum!
Applica semplicemente il [Risolto]! Prova: http://forum.ubuntu-it.org/viewtopic.php?f=70&t=548821
Vuoi qualcosa di piu' dal forum? Prova i miei script: http://forum.ubuntu-it.org/viewtopic.php?f=70&t=597066
Chiusa

Ritorna a “Programmazione”

Chi c’è in linea

Visualizzano questa sezione: 0 utenti iscritti e 4 ospiti