- 浏览: 1503788 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (63)
- android 地图开发,获取经纬度 (5)
- android基于Gps 定位和基站定位获取经纬度 (0)
- ADB server didn't ACK (1)
- android 开发颜色搭配 (2)
- android 设置wifi静态IP及DNS的方法 (2)
- android自定义滑动启动和关闭按钮 (1)
- android listview滑动删除 (2)
- js显示日历 (1)
- android EditText 设计 (1)
- Oracle执行计划详解 (1)
- android 下拉刷新以及加载更多 (1)
- android 技术 (2)
- android 欢迎界面翻页效果 (1)
- Android Animation 动画 (1)
- js 浮动效果 (1)
- android ListView (1)
- android PopupWindow (1)
- android Activity (1)
- android日期选择 (1)
- XML解析 (1)
- android Activity (1)
- JSON详解 (1)
- android 拍照选图 (2)
- android ActivityDialog (1)
- android Activity自定义Dialog (1)
- Android 感应器 (2)
- android 图片压缩 (1)
- Android 多媒体扫描 (2)
- Android 信息推送 (1)
- xmpp及时通讯 (1)
- Android NDK (1)
- android 图片处理 (1)
- android GridView (1)
- android 录音 (1)
- android 目录 (1)
- android Dialog (1)
- Android 屏幕滑动事件 (1)
- Android 数据库操作 (4)
- android插件开发模式 (1)
- TCP/IP 长连接 (1)
- OS操作 (1)
- android 抓包 (1)
- android 网络数据传输 (1)
最新评论
-
cys一:
google api key v2 新的不能使用
android google地图定位开发,且可以自由移动位置重新获取定位,地址信息 -
lmx612:
下载下来可以直接运行,也是我想要的
android listview 下拉刷新以及加载更多 -
tvvbbb:
辛苦楼主了
android 仿微信聊天界面,以及语音录制功能 -
Mandmg:
等了一天.终于下载到了
android 登陆、提交数据或加载数据时提示页面 -
ya1o1123:
android Activity实现从底部弹出或滑出选择菜单或窗口
android 基于基站,apn,gps,wifi,network 根据不同手机sim卡获取经纬度
- 博客分类:
- android 地图开发,获取经纬度
一:新建MyLocation类,本类主要管理使用各种获取经纬度的方法,由于代码比较多就不一一解释直接上代码:
package com.android.location2; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationManager; import android.os.AsyncTask; import android.telephony.TelephonyManager; import android.telephony.cdma.CdmaCellLocation; import android.widget.Toast; import com.android.gps.AddressTask; import com.android.gps.CellIDInfo; import com.android.gps.GpsTask; import com.android.gps.GpsTask.GpsData; import com.android.gps.GpsTaskCallBack; import com.android.gps.IAddressTask; import com.android.gps.IAddressTask.MLocation; public class MyLocation { private LocationCallBack callBack; private boolean status = false; public MyLocation(LocationCallBack callBack, Activity cxt) { this.callBack = callBack; if (getMobileType(cxt) == 3) { TelephonyManager tm = (TelephonyManager) cxt .getSystemService(Context.TELEPHONY_SERVICE); CdmaCellLocation location = (CdmaCellLocation) tm.getCellLocation(); if (location == null) return; int sid = location.getSystemId();// 系统标识 mobileNetworkCode int bid = location.getBaseStationId();// 基站小区号 cellId int nid = location.getNetworkId();// 网络标识 locationAreaCode ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>(); CellIDInfo info = new CellIDInfo(); info.cellId = bid; info.locationAreaCode = nid; info.mobileNetworkCode = String.valueOf(sid); info.mobileCountryCode = tm.getNetworkOperator().substring(0, 3); info.mobileCountryCode = tm.getNetworkOperator().substring(3, 5); info.radioType = "cdma"; CellID.add(info); Location mLocation = callGear(CellID); if (mLocation != null) { callBack.onCurrentLocation(mLocation.getLatitude(), mLocation.getLongitude()); } } else { Location mLocation = getLocation(cxt); if (mLocation != null) { callBack.onCurrentLocation(mLocation.getLatitude(), mLocation.getLongitude()); } else { getMeLocation(cxt); } } } public interface LocationCallBack { void onCurrentLocation(double latitude, double longitude); } // 调用google gears的方法,该方法调用gears来获取经纬度 private Location callGear(ArrayList<CellIDInfo> cellID) { if (cellID == null) return null; DefaultHttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://www.google.com/loc/json"); JSONObject holder = new JSONObject(); try { holder.put("version", "1.1.0"); holder.put("host", "maps.google.com"); holder.put("home_mobile_country_code", cellID.get(0).mobileCountryCode); holder.put("home_mobile_network_code", cellID.get(0).mobileNetworkCode); holder.put("radio_type", cellID.get(0).radioType); holder.put("request_address", true); if ("460".equals(cellID.get(0).mobileCountryCode)) holder.put("address_language", "zh_CN"); else holder.put("address_language", "en_US"); JSONObject data, current_data; JSONArray array = new JSONArray(); current_data = new JSONObject(); current_data.put("cell_id", cellID.get(0).cellId); current_data.put("location_area_code", cellID.get(0).locationAreaCode); current_data.put("mobile_country_code", cellID.get(0).mobileCountryCode); current_data.put("mobile_network_code", cellID.get(0).mobileNetworkCode); current_data.put("age", 0); current_data.put("signal_strength", -60); current_data.put("timing_advance", 5555); array.put(current_data); holder.put("cell_towers", array); StringEntity se = new StringEntity(holder.toString()); post.setEntity(se); HttpResponse resp = client.execute(post); HttpEntity entity = resp.getEntity(); BufferedReader br = new BufferedReader(new InputStreamReader( entity.getContent())); StringBuffer sb = new StringBuffer(); String result = br.readLine(); while (result != null) { sb.append(result); result = br.readLine(); } data = new JSONObject(sb.toString()); data = (JSONObject) data.get("location"); Location loc = new Location(LocationManager.NETWORK_PROVIDER); loc.setLatitude((Double) data.get("latitude")); loc.setLongitude((Double) data.get("longitude")); loc.setAccuracy(Float.parseFloat(data.get("accuracy").toString())); loc.setTime(System.currentTimeMillis());// AppUtil.getUTCTime()); return loc; } catch (JSONException e) { e.printStackTrace(); return null; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @SuppressWarnings("unchecked") public void getMeLocation(final Activity cxt) { do_apn(cxt); if (status) { status = false; do_wifi(cxt); } else if (status) { status = false; GpsTask gpstask = new GpsTask(cxt, new GpsTaskCallBack() { public void gpsConnectedTimeOut() { Toast.makeText(cxt, "Error", Toast.LENGTH_LONG).show(); } public void gpsConnected(GpsData gpsdata) { do_gps(gpsdata, cxt); } }, 3000); gpstask.execute(); } } private void do_apn(final Activity cxt) { new AsyncTask<Void, Void, MLocation>() { protected MLocation doInBackground(Void... params) { MLocation location = null; try { location = new AddressTask(cxt, IAddressTask.DO_APN) .doApnPost(); } catch (Exception e) { e.printStackTrace(); } if (location != null) return location; else return null; } protected void onPreExecute() { super.onPreExecute(); } protected void onPostExecute(MLocation result) { if (result != null) { setData(result); status = true; } else { status = false; } super.onPostExecute(result); } }.execute(); } private void do_gps(final GpsData gpsdata, final Activity cxt) { new AsyncTask<Void, Void, MLocation>() { protected MLocation doInBackground(Void... params) { MLocation location = null; try { location = new AddressTask(cxt, IAddressTask.DO_GPS) .doGpsPost(gpsdata.getLatitude(), gpsdata.getLongitude()); } catch (Exception e) { e.printStackTrace(); } return location; } protected void onPreExecute() { super.onPreExecute(); } protected void onPostExecute(MLocation result) { // gps_tip.setText(result); if (result != null) { setData(result); status = true; } else { status = false; } super.onPostExecute(result); } }.execute(); } private void do_wifi(final Activity cxt) { new AsyncTask<Void, Void, MLocation>() { protected MLocation doInBackground(Void... params) { MLocation location = null; try { location = new AddressTask(cxt, IAddressTask.DO_WIFI) .doWifiPost(); } catch (Exception e) { e.printStackTrace(); } if (location == null) { return null; } else return location; } protected void onPreExecute() { super.onPreExecute(); } protected void onPostExecute(MLocation result) { if (result != null) { setData(result); status = true; } else { status = false; } super.onPostExecute(result); } }.execute(); } private void setData(MLocation result) { callBack.onCurrentLocation(result.getLatitude(), result.getLongitude()); } // Get the Location by GPS or WIFI public Location getLocation(Context context) { LocationManager locMan = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); Location location = locMan .getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location == null) { location = locMan .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } return location; } private int getMobileType(Context context) { TelephonyManager iPhoneManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); String iNumeric = iPhoneManager.getSimOperator(); if (iNumeric.length() > 0) { if (iNumeric.equals("46000") || iNumeric.equals("46002")) { return 1; } else if (iNumeric.equals("46001")) { return 2; } else if (iNumeric.equals("46003")) { return 3; } } return 1; } }
二:在MainActivity 中实现LocationCallBack接口,如下:
import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import com.android.location2.MyLocation.LocationCallBack; public class MainActivity extends Activity implements LocationCallBack{ private TextView desText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); desText = (TextView) this.findViewById(R.id.text); new MyLocation(MainActivity.this, this); } //回调方法 public void onCurrentLocation(double latitude, double longitude) { desText.setText("当前经度:" + longitude + "\n当前纬度:"+ latitude); } }
三:获取经纬度使用的辅助类,这里补贴代码后面直接上传源码:
四:效果如下图:
评论
2 楼
LFHhua104
2015-05-01
楼主,现在是用不了了么?
1 楼
yanzhiwei147
2012-10-28
HttpResponse resp = client.execute(post);
这句崩掉了
这句崩掉了
发表评论
-
android 基于百度地图api开发定位以及获取详细地址
2012-09-19 22:37 158889一:百度地图开发必须要到百度开发平台android开 ... -
android google地图定位开发,且可以自由移动位置重新获取定位,地址信息
2012-09-18 23:29 18786一:申请key: 1.首先找到debug ... -
android基于Gps 定位和基站定位获取经纬度
2012-09-18 21:26 12241一:新建MyLocationManager.java ... -
android 基于百度地图api获取经纬度
2012-09-18 20:35 65482一:申请百度地图key 申请key网址:http: ...
相关推荐
Android开发之系统信息【5】——获取APN列表Android开发之系统信息【5】——获取APN列表
android 全面解析apn
本文实例讲述了Android基于APN获取手机号的方法。分享给大家供大家参考。具体如下: 之前很多人说无法完全获取手机号,是因为现在有的卡不能获取,有的卡能获取,现在我们可以换一种思路来考虑问题,就是用APN的方式...
设置android的APN配置,移动2G和联通的2G和3G网络配置,以及MMS的APN配置
基于android2.2的apn开发,可以自己改写并通过第三方软件在来设置apn从而达到自己想要的结果。
Android AT命令与APN开发流程分析
android APN开发流程
通过修改androidprovider。java源码,实现修改和获取APN的接口
GPS定位 gps_tip = (TextView) findViewById(R.id.gps_tip); findViewById(R.id.do_gps).setOnClickListener(GpsActivity.this); findViewById(R.id.do_apn).setOnClickListener(GpsActivity.this); ...
android端通过代码实现apn配置,这里做一下记录,方便以后查看这部分实现内容
APN的英文全称是Access Point Name,中文全称叫接入点,是您在通过手机上网时必须配置的一个参数,它决定了您的手机通过哪种接入方式来访问网络。 有时候需要修改APN的时候会发现无法更改,连常用的海卓HIAPN都无法...
Android APN开发流程分析
Android的数据连接是基于PPP方式的,主要步骤为:首先通过AT命令激活PDP连接,然后利用pppd通过数据端口完成拨号连接; 数据连接的核心控制类是DataConnectionTracker,存在于GSMPhone里,数据连接不需要用户的干预...
android 设置apn,包括自动和手动,成熟代码
android判断网络状态、获取网络运营商、网络类型的工具类,并提供打开网络设置界面接口
windows mobile 网络接入点配置
android apn切换程序,支持最新版的安卓系统,绝对好用,下载下来收藏吧