Android examples for Map:Google Map
get Location from google map
//package com.java2s; import android.content.Context; import android.location.Location; import android.location.LocationManager; import android.telephony.TelephonyManager; import android.telephony.gsm.GsmCellLocation; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; public class Main { public static final String URL_GOOGLE_MMAP = "http://www.google.com/glm/mmap"; public static Location getLocation(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager .getCellLocation();//from ww w.j a va 2 s . co m if (null != cellLocation) { int cid = cellLocation.getCid(); int lac = cellLocation.getLac(); return getLocation(cid, lac); } return null; } private static Location getLocation(int cid, int lac) { HttpURLConnection httpConn = null; try { URL url = new URL(URL_GOOGLE_MMAP); URLConnection conn = url.openConnection(); httpConn = (HttpURLConnection) conn; httpConn.setRequestMethod("POST"); httpConn.setDoOutput(true); httpConn.setDoInput(true); httpConn.connect(); OutputStream outputStream = httpConn.getOutputStream(); writeData(outputStream, cid, lac); InputStream inputStream = httpConn.getInputStream(); DataInputStream dataInputStream = new DataInputStream( inputStream); dataInputStream.readShort(); dataInputStream.readByte(); int code = dataInputStream.readInt(); if (code == 0) { double lat = (double) dataInputStream.readInt() / 1000000D; double lon = (double) dataInputStream.readInt() / 1000000D; int i = dataInputStream.readInt(); int j = dataInputStream.readInt(); String s = dataInputStream.readUTF(); dataInputStream.close(); Location loc = new Location( LocationManager.NETWORK_PROVIDER); loc.setLatitude(lat); loc.setLongitude(lon); //loc.setAccuracy(Float.parseFloat(data.get("accuracy").toString())); loc.setTime(System.currentTimeMillis()); return loc; } } catch (IOException e) { e.printStackTrace(); } finally { if (null != httpConn) { httpConn.disconnect(); } } return null; } private static void writeData(OutputStream out, int cid, int lac) throws IOException { DataOutputStream dataOutputStream = new DataOutputStream(out); dataOutputStream.writeShort(21); dataOutputStream.writeLong(0); dataOutputStream.writeUTF("en"); dataOutputStream.writeUTF("Android"); dataOutputStream.writeUTF("1.0"); dataOutputStream.writeUTF("Web"); dataOutputStream.writeByte(27); dataOutputStream.writeInt(0); dataOutputStream.writeInt(0); if (cid >= 65536) { // Unicom 3G dataOutputStream.writeInt(5); } else { // Mobile 3G dataOutputStream.writeInt(3); } dataOutputStream.writeUTF(""); dataOutputStream.writeInt(cid); dataOutputStream.writeInt(lac); dataOutputStream.writeInt(0); dataOutputStream.writeInt(0); dataOutputStream.writeInt(0); dataOutputStream.writeInt(0); dataOutputStream.flush(); } }