android gps定位

  • t1_465901
    了解作者
  • 113.1KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-13 04:16
    上传日期
androidgps定位.三种方式定位。
gps定位.zip
内容介绍
package com.maxtech.common.gps; import java.io.BufferedReader; import java.io.InputStreamReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.json.JSONArray; import org.json.JSONObject; import android.app.Activity; import android.content.Context; import android.net.wifi.WifiManager; import android.telephony.TelephonyManager; import android.telephony.gsm.GsmCellLocation; public abstract class IAddressTask { protected Activity context; public IAddressTask(Activity context) { this.context = context; } public abstract HttpResponse execute(JSONObject params) throws Exception; public MLocation doWifiPost() throws Exception { return transResponse(execute(doWifi())); } public MLocation doApnPost() throws Exception { return transResponse(execute(doApn())); } public MLocation doGpsPost(double lat, double lng) throws Exception { return transResponse(execute(doGps(lat, lng))); } private MLocation transResponse(HttpResponse response) { MLocation location = null; if (response.getStatusLine().getStatusCode() == 200) { location = new MLocation(); HttpEntity entity = response.getEntity(); BufferedReader br; try { br = new BufferedReader(new InputStreamReader( entity.getContent())); StringBuffer sb = new StringBuffer(); String result = br.readLine(); while (result != null) { sb.append(result); result = br.readLine(); } JSONObject json = new JSONObject(sb.toString()); JSONObject lca = json.getJSONObject("location"); location.Access_token = json.getString("access_token"); if (lca != null) { if(lca.has("accuracy")) location.Accuracy = lca.getString("accuracy"); if(lca.has("longitude")) location.Longitude = lca.getDouble("longitude"); if(lca.has("latitude")) location.Latitude = lca.getDouble("latitude"); if(lca.has("address")) { JSONObject address = lca.getJSONObject("address"); if (address != null) { if(address.has("region")) location.Region = address.getString("region"); if(address.has("street_number")) location.Street_number = address .getString("street_number"); if(address.has("country_code")) location.Country_code = address .getString("country_code"); if(address.has("street")) location.Street = address.getString("street"); if(address.has("city")) location.City = address.getString("city"); if(address.has("country")) location.Country = address.getString("country"); } } } } catch (Exception e) { e.printStackTrace(); location = null; } } return location; } private JSONObject doGps(double lat, double lng) throws Exception { JSONObject holder = new JSONObject(); holder.put("version", "1.1.0"); holder.put("host", "maps.google.com"); holder.put("address_language", "zh_CN"); holder.put("request_address", true); JSONObject data = new JSONObject(); data.put("latitude", lat); data.put("longitude", lng); holder.put("location", data); return holder; } private JSONObject doApn() throws Exception { JSONObject holder = new JSONObject(); holder.put("version", "1.1.0"); holder.put("host", "maps.google.com"); holder.put("address_language", "zh_CN"); holder.put("request_address", true); TelephonyManager tm = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation(); int cid = gcl.getCid(); int lac = gcl.getLac(); int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0, 3)); int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3, 5)); JSONArray array = new JSONArray(); JSONObject data = new JSONObject(); data.put("cell_id", cid); data.put("location_area_code", lac); data.put("mobile_country_code", mcc); data.put("mobile_network_code", mnc); array.put(data); holder.put("cell_towers", array); return holder; } private JSONObject doWifi() throws Exception { JSONObject holder = new JSONObject(); holder.put("version", "1.1.0"); holder.put("host", "maps.google.com"); holder.put("address_language", "zh_CN"); holder.put("request_address", true); WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if(wifiManager.getConnectionInfo().getBSSID() == null) { throw new RuntimeException("bssid is null"); } JSONArray array = new JSONArray(); JSONObject data = new JSONObject(); data.put("mac_address", wifiManager.getConnectionInfo().getBSSID()); data.put("signal_strength", 8); data.put("age", 0); array.put(data); holder.put("wifi_towers", array); return holder; } public static class MLocation { public String Access_token; public double Latitude; public double Longitude; public String Accuracy; public String Region; public String Street_number; public String Country_code; public String Street; public String City; public String Country; @Override public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append("Access_token:" + Access_token + "\n"); buffer.append("Region:" + Region + "\n"); buffer.append("Accuracy:" + Accuracy + "\n"); buffer.append("Latitude:" + Latitude + "\n"); buffer.append("Longitude:" + Longitude + "\n"); buffer.append("Country_code:" + Country_code + "\n"); buffer.append("Country:" + Country + "\n"); buffer.append("City:" + City + "\n"); buffer.append("Street:" + Street + "\n"); buffer.append("Street_number:" + Street_number + "\n"); return buffer.toString(); } } }
评论
    相关推荐
    • Android学习
      Android学习Android学习Android学习Android学习Android学习Android学习
    • Android678
      Android678Android678Android678Android678Android678Android678Android678Android678Android678Android678
    • android
      掌握Android Studio开发环境的搭建步骤,能够独立搭建 Android Studio开发环境
    • android图片
      android精美小图片android精美小图片android精美小图片android精美小图片
    • android课件
      清华老师上课的android课件。ch_01android概述ch_02android开发基础ch_03资源的访问ch_04android用户界面ch_05Ativity和Intent。其他自己看。
    • AndroidKiller
      安卓App反编译工具AndroidKiller\AndroidKiller\AndroidKiller
    • AndroidSample
      Android示例 这是一个示例Android应用,用于降低Google Play服务和Android Volley的使用率。 有关更多详细信息,请参阅以下链接: Google Play服务: Android凌空抽射:
    • android source
      android 源代码 source android 源代码 source android 源代码 source android 源代码 source android 源代码 source android 源代码 source android 源代码 source
    • Android 教程
      Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android ...
    • android资料
      Android程序员指南 Android_UI开发专题 Android程序开发初级教程 android的五大类各种常用类的资料整理 Android高手进阶教程