Tag Cloud

accordion (1) admin (1) administration (1) android (2) angel love (2) answer (1) app (1) archangel (2) auto complete (1) axis2 json (1) backup (1) barcode (3) bb (1) binary decimal convert c (1) blackberry (9) boylevantz (1) browser error (1) c (1) C# (1) career (1) certification (1) ci (1) codeigniter (1) color (1) connection (1) contenga (1) create (1) csharp (1) css (1) database (2) db (1) demo (1) dev (2) developer (2) download (1) eclipse (1) enterprise deploy (1) eval (1) evo (1) expression (2) facelets (1) free (1) golden ratio (1) grant (1) hash md5 encrypt encryption (1) heap size (1) hibernate (1) html (1) ibm (3) index.php (1) installation manager (1) ios (1) java (14) javascript (3) jawab (1) job (1) jquery (1) jquery mobile (2) js (2) jsf (2) juel (1) kerjaan (1) kernel (1) language (1) launchpad (1) linux (1) list (1) listfield (1) location (1) log (1) log4j (1) logging (1) lowongan (1) magang (1) maven (1) memory (1) message (1) mobile (3) mysql (3) native (1) opportunity (1) orion (1) out of memory (1) php (2) pin (1) pom.xml maven pom sort sortpom mvn (1) postgre (1) program (1) programmer (2) programming (1) psql (1) Q&A (1) qrcode barcode java (1) question (1) remote (1) remove index.php (1) servlet (1) simple (1) solusi (1) spring (1) swap (1) tab (1) tanya (1) tcp rinetd linux suse redirection (1) tips (1) Tomcat (1) tooltip (1) training (1) ui (1) url (1) user (1) user interface (1) vacancy (3) web (4) webworks (1) worklight (3) zxing (1)
Showing posts with label location. Show all posts
Showing posts with label location. Show all posts

Monday, October 17, 2011

Get Latitude and Longitude based on provided data using Java

We can get Latitude and Longitude based on MCC, MNC, LAC, Cell ID


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * GetLocation provide mechanism to get location based on MCC, MNC, LAC, and Cell ID
 *
 * @author Ali Irawan
 * @version 1.0
 */
public class GetLocation {

    /**
     * Get location  based on MCC, MNC, LAC, and Cell ID
     *
     * @param mcc mobile country code
     * @param mnc mobile network code
     * @param lac location area code
     * @param cellId cell id
     * @param requestAddress true if want to get address
     * @return JSON String representation of position
     *      * @throws IOException
     */
    @SuppressWarnings("unused")
    public static String getLocation(String mcc, String mnc, String lac,
            String cellId, boolean requestAddress) throws IOException {

        final String data = "{\"cell_towers\": [{\"location_area_code\": \""
                + lac + "\", \"mobile_network_code\": \"" + mnc
                + "\", \"cell_id\": \"" + cellId
                + "\", \"mobile_country_code\": \"" + mcc
                + "\"}], \"version\": \"1.1.0\", \"request_address\": \""
                + requestAddress + "\"}";
        final String url = "https://www.google.com/loc/json";

        URL anUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) anUrl.openConnection();

        conn.setRequestMethod("GET");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        conn.setAllowUserInteraction(false);
        conn.setRequestProperty("Content-type", "application/jsonrequest");

        OutputStream out = conn.getOutputStream();
        out.write(data.getBytes());
        out.close();

        InputStream in = conn.getInputStream();

        StringBuilder builder = new StringBuilder();
        Reader reader = new InputStreamReader(in);
        char[] buf = new char[1024];
        int read = 0;
        while ((read = reader.read(buf)) >= 0) {
            builder.append(String.valueOf(buf, 0, read));
        }
        in.close();
        conn.disconnect();

        return builder.toString();
    }
}


Example of use:

String location = GetLocation.getLocation("510", "89", "9250", "851", true);
       
System.out.println(location);

Output:

{
   "location":
      {  
         "latitude":-6.2452443,
         "longitude":106.7908857,
         "address": {
                "country":"Indonesia",
                "country_code":"ID",
                "region":"Jakarta Capital Region",
                "city":"Jakarta Capital Region",
                "street":"Jalan Gandaria Tengah 4",
                "postal_code":"12240"
             },
         "accuracy":713.0
       },
   "access_token":"2:C5RroEdSGjSfnq9X:7oAhDHrp_5xzgTeU"
}