javajsongeolocationgiscellid

Using The Google Maps Geolocation API


I am Using following code to get the lat. long. by providing MCC, MNC I am using Google Maps Geo-location API for this but I am getting same results (lat/long) for different MCC/MNC values . Even when I am requesting blank json I am getting same results(lat/long). Where am I going wrong ?

public class CellID {

    public static void main(String[] args) {
        try{
            putDataToServer("https://www.googleapis.com/geolocation/v1/geolocate?key=mykey",null);
        }
        catch(Throwable throwable){
            System.out.println("Error");
        }
    }

    public static String putDataToServer(String url,JSONObject returnedJObject) throws Throwable
    {

        HttpPost request = new HttpPost(url);

        JSONStringer json = (JSONStringer) new JSONStringer()
        .object() 
         .key("mobileCountryCode").value(504)   
         .key("mobileNetworkCode").value(0)
         .key("locationAreaCode").value(0)
         .key("cellID").value(0)
        .endObject();



        System.out.println("json"+json.toString());

        StringEntity entity = new StringEntity(json.toString(), "UTF-8");


                 request.setEntity(entity); 


        HttpResponse response =null;
        HttpClient httpClient = new DefaultHttpClient();

        try{

            response = httpClient.execute(request); 
        }
        catch(SocketException se)
        {
            throw se;
        }

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        //Displaying the response received.
        String line = "";
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
            if (line.startsWith("Auth=")) {
                String key = line.substring(5);
                // Do something with the key
            }

        }
        return response.getEntity().toString();

    }

}

Solution

  • Is your JSON request object complete? I mean it looks like the keys you are using are part of a single "tower" description, but that this is just part of the larger request body, which should be formatted like:

    {
      "homeMobileCountryCode": 310,
      "homeMobileNetworkCode": 410,
      "radioType": "gsm",
      "carrier": "Vodafone",
      "cellTowers": [
       // See the Cell Tower Objects section below.
      ],
      "wifiAccessPoints": [
        // See the WiFi Access Point Objects section below.
      ]
    }
    

    Where the tower objects are formatted like:

    {'cellTowers': [
      {
        'cellId': 42,
        'locationAreaCode': 415,
        'mobileCountryCode': 310,
        'mobileNetworkCode': 410,
        'age': 0,
        'signalStrength': -60,
        'timingAdvance': 15
      }
    ]}
    

    I guess I'm missing how your json object gets turned into the complete one?

    https://developers.google.com/maps/documentation/business/geolocation/