Android 手机定位

来源:互联网 发布:linux tail -f 编辑:程序博客网 时间:2024/04/29 23:05

android手机定位在中国还是有些坑的。
首先android的定位方式分为三种:GPS,NET_WORK,PASSIVE

具体问题代码见:


1.获取定位管理服务:

LocationManager mgLocation=(LocationManager)MainActivity.this.getSystemService(Context.LOCATION_SERVICE);

2.获取定位
//GPS定位

Location location_1=mgLocation.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//网络基站定位

Location location_2=mgLocation.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

//手机服务商定位

Location location_3=mgLocation.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);



其中坑的是GPS定位,在中国,你在室内开发时,你的手机根本就没法获取位置信息

如果想通过精确的GPS获取定位需要结合百度SDK,百度地图,高德地图等协同显示


获取权限:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.INTERNET" />


//设定实时定位间隔

mgLocation.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 1000, 0, MainActivity.this);

//其中MainActivity.this表示的是LocationListener,

因为:

MainActivity extends Activity implements LocationListener

//复写方法:

@Override

public void onLocationChanged(Location location)

location为定位成功后的位置


打开手机GPS的Intent:
Intent intent=new Intent();

intent.setAction(Setting.Action_LOCATION_SOURCE_SETTINGS);

startActivity(intent);


最后介绍一下通过WIFI定位:

首先获取wifi的mac地址

然后将mac地址发送给google,获取google分析过来的地理位置

public classWiFiInfoManager implements Serializable {
     privatestaticfinal long serialVersionUID= -4582739827003032383L;
     privateContext context;
     publicWiFiInfoManager(Context context) {
         super();
         this.context = context;
       }
     publicWifiInfo getWifiInfo() {
     WifiManager manager = (WifiManager)context
            .getSystemService(Context.WIFI_SERVICE);
     WifiInfo info =newWifiInfo();
     info.mac =manager.getConnectionInfo().getBSSID();
     Log.i( TAG , WIFI MACis: + info.mac);
           return info;
         }
     publicclassWifiInfo {
     publicString mac;
     publicWifiInfo() {
        super();
           }
         }
       }


         publicstaticLocation getWIFILocation(WifiInfo wifi) {
            if(wifi ==null) {
              Log.i( TAG , wifiisnull. );
              returnnull;
            }
            DefaultHttpClient client = newDefaultHttpClient();
            HttpPost post =newHttpPost( http://www.google.com/loc/json );
            JSONObject holder =newJSONObject();
            try{
              holder.put( version ,1.1.0);
              holder.put( host , maps.google.com );
              JSONObject data;
              JSONArray array =newJSONArray();
              if(wifi.mac !=null wifi.mac.trim().length() 0) {
                data =newJSONObject();
               data.put( mac_address , wifi.mac);
               data.put( signal_strength ,8);
                data.put( age ,0);
                array.put(data);
              }
              holder.put( wifi_towers ,array);
              Log.i( TAG , request json: + holder.toString());
              StringEntity se = newStringEntity(holder.toString());
              post.setEntity(se);
              HttpResponse resp =client.execute(post);
              intstate =resp.getStatusLine().getStatusCode();
              if(state == HttpStatus.SC_OK) {
                HttpEntity entity =resp.getEntity();
                if(entity !=null) {
                  BufferedReader br = newBufferedReader(
                      newInputStreamReader(entity.getContent()));
                  StringBuffer sb = newStringBuffer();
                  String resute = ;
                  while((resute =br.readLine()) !=null) {
                    sb.append(resute);
                  }
                  br.close();
                  Log.i( TAG , response json: + sb.toString());
                  data = newJSONObject(sb.toString());
                  data = (JSONObject)data.get( location );
                  Location loc = newLocation(
                     android.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());
                  returnloc;
                }else{
                  returnnull;
                }
              }else{
                Log.v( TAG , state + );
                returnnull;
              }
            }catch(Exception e) {
              Log.e( TAG ,e.getMessage());
              returnnull;
            }
          }

上面这个方法仅作为学习使用,因为伟大的墙的原因,google 的网站我们是连不上去滴


注:

LocationManager.PASSIVE_PROVIDER定位貌似只能使用一次,不能连续监听,下次使用还需要重新调用方法

具体并未尝试太多,有新发现可以共同讨论



0 0