您的位置:首页 > 移动开发 > Android开发

ym——Android从零开始(34)(基站、wifi、GPS定位)(新)

2014-08-08 14:26 483 查看
[b]转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103[/b]),谢谢支持!


基站、wifi、GPS定位

基站定位:误差大

Wifi定位:是网段定位

GPS:精准





基站定位

//基站定位 --》 信号塔 --》 打电话  --》 TelelphoneManager
   public void cellLocation(View v){
           TelephonyManager tm =
(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
           
           //联通
           GsmCellLocation location =(GsmCellLocation) tm.getCellLocation();
           
           //基站的id
           int cid = location.getCid();
           
           //区域标示
           int lac = location.getLac();
           
           //网络的控制者(mcc + mnc)  
           Stringoperator = tm.getNetworkOperator();
           
           //mcc 国家编码
           String mcc = operator.substring(0, 3);
           
           //运营商编码
           String mnc = operator.substring(3);
           
   
           Log.i("i","cid:"+cid+"lac:"+lac+",mcc:"+mcc+",mnc:"+mnc);
           //获取经纬度  :
           //网络上有很多公开的api提供给你,你给一个api上面的值,发送过去
           //相应给你经纬度信息   
           //一般对外公开的api都是以json的形式接收和返回数据
           
           try {
                     HttpClienthttpClient = new DefaultHttpClient();
                     
                     HttpPostrequest = new HttpPost("http://www.google.com/loc/json");
                     //要发送的数据怎么组拼
                     JSONObjectobject = new JSONObject();
               object.put("cell_id", cid);
              object.put("location_area_code", lac);
              object.put("mobile_country_code", mcc);
              object.put("mobile_network_code", mnc);
               object.put("version","1.1.0");
               object.put("host","maps.google.com");
              object.put("address_language", "zh_CN");
               object.put("request_address",true);
               object.put("radio_type","gsm");
               
               //把数据放入到Request里面以实体的形式
               StringEntity entity = newStringEntity(object.toString());
               request.setEntity(entity);
                     
                     HttpResponseresponse = httpClient.execute(request);
                     intstatuscode = response.getStatusLine().getStatusCode();
                     if(statuscode== HttpStatus.SC_OK){
                            InputStream is =response.getEntity().getContent();
                           
                            //返回过来的数据怎么解析
                            ByteArrayOutputStream bos = newByteArrayOutputStream();
                            byte[] buffer = new byte[1024];
                            int len = 0;
                            while((is.read(buffer)) != -1){
                                   bos.write(buffer,0, len);
                            }
                            String content = bos.toString();
                            bos.close();
                            is.close();
                           
                            JSONObject jsonObject = newJSONObject(content);
                           
                            //定位信息
                            JSONObject jsLocation=jsonObject.getJSONObject("location");
                     //获取纬度
                     String latitude=jsLocation.getString("latitude");
                     //获取经度
                     String longitude=jsLocation.getString("longitude");
                     
                     //地理位置信息
                     JSONObject jsAddress = jsonObject.getJSONObject("address");
                     String country = jsAddress.getString("country");
                     String region=jsAddress.getString("region");
                     String city=jsAddress.getString("city");
                     String street=jsAddress.getString("street");
                     String street_number=jsAddress.getString("street_number");
                     
                     //基站定位:误差比较大
                     
                     }
              } catch (Exception e) {
                     //TODO Auto-generated catch block
                     e.printStackTrace();
              }
           
           
    }


Wifi定位

//wifi
   public void wifiocation(View v){
              WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
              
              //得到连接到的wifi
              WifiInfo wifiInfo =wm.getConnectionInfo();
              //mac地址
              String mac = wifiInfo.getMacAddress();
              //服务的id
              String ssid = wifiInfo.getSSID();
              
              try {
                     HttpClienthttpClient = new DefaultHttpClient();
                     
                     HttpPostrequest = new HttpPost("http://www.google.com/loc/json");
                     //要发送的数据怎么组拼
                     JSONObjectobject = new JSONObject();
                     object.put("mac_address",mac);
                     object.put("ssid",ssid);
               object.put("version","1.1.0");
               object.put("host", "maps.google.com");
              object.put("address_language", "zh_CN");
               object.put("request_address",true);
               object.put("radio_type","gsm");
               
               //把数据放入到Request里面以实体的形式
               StringEntity entity = newStringEntity(object.toString());
               request.setEntity(entity);
                     
                     HttpResponseresponse = httpClient.execute(request);
                     intstatuscode = response.getStatusLine().getStatusCode();
                     if(statuscode== HttpStatus.SC_OK){
                            InputStream is =response.getEntity().getContent();
                           
                            //返回过来的数据怎么解析
                            ByteArrayOutputStream bos = newByteArrayOutputStream();
                            byte[] buffer = new byte[1024];
                            int len = 0;
                            while((is.read(buffer)) != -1){
                                   bos.write(buffer,0, len);
                            }
                            String content = bos.toString();
                            bos.close();
                            is.close();
                           
                            JSONObject jsonObject = newJSONObject(content);
                           
                            //定位信息
                            JSONObject jsLocation=jsonObject.getJSONObject("location");
                     //获取纬度
                     String latitude=jsLocation.getString("latitude");
                      //获取经度
                     String longitude=jsLocation.getString("longitude");
                     
                     //地理位置信息
                     JSONObject jsAddress = jsonObject.getJSONObject("address");
                     String country = jsAddress.getString("country");
                     String region=jsAddress.getString("region");
                     String city=jsAddress.getString("city");
                     String street=jsAddress.getString("street");
                     String street_number=jsAddress.getString("street_number");
                     
                     //wifi定位:其实是网段定位
                     
                     
                     
                     }
              } catch (Exception e) {
                     //TODO Auto-generated catch block
                     e.printStackTrace();
              }
       }


GPS定位

//gps定位  :精确度最高   
   public void gpsLocation(View v){
           //定位服务
           LocationManager lm =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
           //得到所有的定位方式
           List<String> providers =lm.getAllProviders();
           for(String provider:providers){
                  Log.i("i",provider);
           }
           
           //得到指定的定位方式
           //LocationProvider provider =lm.getProvider("gps");
           
           //lm.setTestProviderEnabled(LocationManager.GPS_PROVIDER,true);
           
           //监听定位的改变(定位也是很好性能)  定位也需要时间
           lm.requestLocationUpdates(
                         LocationManager.GPS_PROVIDER,//指定定位方式
                         60000,//更新的最小周期
                         10,//更新的最短距离  是0就表示不关心距离的变化
                         new MyLocationListener());
    }
   
   
   private class MyLocationListener implements LocationListener{
           //位置的改变
              @Override
              public void onLocationChanged(Locationlocation) {
                     //TODO Auto-generated method stub
                     //从位置的身上就能得到经纬度
                     //经度
                     doublelongitude = location.getLongitude();
                     //维度
                     doublelatitude = location.getLatitude();
                     
                     try{
                            HttpGet httpGet=
newHttpGet("http://maps.google.com/maps/api/geocode/json?latlng=" +latitude+","+longitude+"&sensor=true&language=zh-CN");
                            HttpClient client=newDefaultHttpClient();
                            HttpResponse httpResponse;
                            StringBuffer stringBuffer=newStringBuffer();
                            httpResponse=client.execute(httpGet);
                            HttpEntityhttpEntity=httpResponse.getEntity();
                            BufferedReader br =
newBufferedReader(new InputStreamReader(httpEntity.getContent()));           
                            String str ="";
                            while ((str=br.readLine())!=null) {
                                   stringBuffer.append(str);
                            }
                            JSONObject jsonObject=newJSONObject(stringBuffer.toString());
                            //获取地址
                            String address=((JSONArray)jsonObject.get("results")).getJSONObject(0).getString("formatted_address");
                            Log.i("i", "地址"+address);
                     }catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                     }     
              }
              //状态的改变
              @Override
              public void onStatusChanged(Stringprovider, int status, Bundle extras) {
                     //TODO Auto-generated method stub
                     
              }
              //定位可用
              @Override
              public void onProviderEnabled(Stringprovider) {
                     //TODO Auto-generated method stub
                     
              }
              //定位不可用
              @Override
              public void onProviderDisabled(Stringprovider) {
                     //TODO Auto-generated method stub
                     
              }
           
}


这次主要是学习如何调用网络上的API

传入所需参数,得到一个json对象,先拼接成String

在用JSONObject解析

可以使用webxml.com.cn

有很多API可供使用

课后问题

1.Android支持那些定位方式?

基站、wifi、 GPS

2.获取经纬度的步骤:1 2 3

基站、wifi

拼接URI

连接网络API

得到数据

GPS

得到定位服务

监听定位改变

实现LocationListener

得到经纬度

连接网络API

得到使定位的实际地址

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: