| WIFI定位其實和基站定位都差不多,只需要把WIFI的MAC地址取到傳給google就行了,下面是具體實現(xiàn)! 
 01.importjava.io.Serializable;02.03.importandroid.content.Context;04.importandroid.net.wifi.WifiManager;05.importandroid.util.Log;06.07./**08.* @author yangzhiqiang09.* 10.*/11.publicclassWiFiInfoManager implementsSerializable {12.13./**14.* 15.*/16.privatestaticfinallongserialVersionUID = -4582739827003032383L;17.18.privateContext context;19.20.publicWiFiInfoManager(Context context) {21.super();22.this.context = context;23.}24.25.publicWifiInfo getWifiInfo() {26.WifiManager manager = (WifiManager) context27..getSystemService(Context.WIFI_SERVICE);28.WifiInfo info = newWifiInfo();29.info.mac = manager.getConnectionInfo().getBSSID();30.Log.i("TAG", "WIFI MAC is:"+ info.mac);31.returninfo;32.}33.34.publicclassWifiInfo {35.36.publicString mac;37.38.publicWifiInfo() {39.super();40.}41.}42.43.}上面是取到WIFI的mac地址的方法,下面是把地址發(fā)送給google服務器,代碼如下: 
 01.publicstaticLocation getWIFILocation(WifiInfo wifi) {02.if(wifi == null) {03.Log.i("TAG", "wifi is null.");04.returnnull;05.}06.DefaultHttpClient client = newDefaultHttpClient();07.HttpPost post = newHttpPost("08.JSONObject holder = newJSONObject();09.try{10.holder.put("version", "1.1.0");11.holder.put("host", "maps.google.com");12.13.JSONObject data;14.JSONArray array = newJSONArray();15.if(wifi.mac != null&& wifi.mac.trim().length() > 0) {16.data = newJSONObject();17.data.put("mac_address", wifi.mac);18.data.put("signal_strength", 8);19.data.put("age", 0);20.array.put(data);21.}22.holder.put("wifi_towers", array);23.Log.i("TAG", "request json:"+ holder.toString());24.StringEntity se = newStringEntity(holder.toString());25.post.setEntity(se);26.HttpResponse resp = client.execute(post);27.intstate = resp.getStatusLine().getStatusCode();28.if(state == HttpStatus.SC_OK) {29.HttpEntity entity = resp.getEntity();30.if(entity != null) {31.BufferedReader br = newBufferedReader(32.newInputStreamReader(entity.getContent()));33.StringBuffer sb = newStringBuffer();34.String resute = "";35.while((resute = br.readLine()) != null) {36.sb.append(resute);37.}38.br.close();39.40.Log.i("TAG", "response json:"+ sb.toString());41.data = newJSONObject(sb.toString());42.data = (JSONObject) data.get("location");43.44.Location loc = newLocation(45.android.location.LocationManager.NETWORK_PROVIDER);46.loc.setLatitude((Double) data.get("latitude"));47.loc.setLongitude((Double) data.get("longitude"));48.loc.setAccuracy(Float.parseFloat(data.get("accuracy")49..toString()));50.loc.setTime(System.currentTimeMillis());51.returnloc;52.} else{53.returnnull;54.}55.} else{56.Log.v("TAG", state + "");57.returnnull;58.}59.60.} catch(Exception e) {61.Log.e("TAG", e.getMessage());62.returnnull;63.}64.} | 
|  |