2015/9/9/4

android 定位的几种方式介绍

android 定位一般有四种方法,这四种方式分别是:GPS定位,WIFI定准,基站定位,AGPS定位,

GPS定位

(1) Android GPS:需要GPS硬件支持,直接和卫星交互来获取当前经纬度,这种方式需要手机支持GPS模块(现在大部分的智能机应该都有了)。通过GPS方式准确度是最高的,但是它的缺点也非常明显:1,比较耗电;2,绝大部分用户默认不开启GPS模块;3,从GPS模块启动到获取第一次定位数据,可能需要比较长的时间;4,室内几乎无法使用。这其中,缺点2,3都是比较致命的。需要指出的是,GPS走的是卫星通信的通道,在没有网络连接的情况下也能用。要实用Adnroid平台的GPS设备,首先需要添加上权限,所以需要添加如下权限:

uses-permission android:name= android.permission.ACCESS_FINE_LOCATION   /uses-permission

简单的实例

要先判断GPS是否已经开启,再去得到他。

package com.zsz.develop.gpsdemo;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class GPSActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gps);
    openGPSSettings();
    getLocation();
}


private void openGPSSettings() {
    LocationManager alm = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);
    if (alm
            .isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
        Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT)
                .show();
        return;
    }

    Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
    startActivityForResult(intent, 0); //此为设置完成后返回到获取界面

}

private void getLocation() {
    // 获取位置管理服务
    LocationManager locationManager;
    String serviceName = Context.LOCATION_SERVICE;
    locationManager = (LocationManager) this.getSystemService(serviceName);
    // 查找到服务信息
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗

    String provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息
    if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    public void requestPermissions(@NonNull String[] permissions, int requestCode)
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for Activity#requestPermissions for more details.
        return;
    }
    Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
    updateToNewLocation(location);
    // 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米
//        locationManager.requestLocationUpdates(provider, 100 * 1000, 500,
//                locationListener);
}
private void updateToNewLocation(Location location) {

    TextView tv1;
    tv1 = (TextView) this.findViewById(R.id.tv1);
    if (location != null) {
        double  latitude = location.getLatitude();
        double longitude= location.getLongitude();
        tv1.setText("维度:" +  latitude+ "\n经度" + longitude);
    } else {
        tv1.setText("无法获取地理信息");
    }

}
}

(2)Android 基站定位:

Android 基站定位只要明白了基站/WIFI定位的原理,自己实现基站/WIFI定位其实不难。

基站定位一般有几种,

第一种是利用手机附近的三个基站进行三角定位,由于每个基站的位置是固定的,利用电磁波在这三个基站间中转所需要时间来算出手机所在的坐标;

第二种则是利用获取最近的基站的信息,其中包括基站 id,location area code、mobile country code、mobile network code和信号强度,将这些数据发送到google的定位web服务里,就能拿到当前所在的位置信息,误差一般在几十米到几百米之内。其中信号强度这个数据很重要,这里笔者就不多做解释了,

直接给出一个文章,这个文章写的非常好,http://www.cnblogs.com/rayee/archive/2012/02/02/2336101.html

(3)Android Wifi定位 ##:

根据一个固定的WifiMAC地址,通过收集到的该Wifi热点的位置,然后访问网络上的定位服务以获得经纬度坐标。因为它和基站定位其实都需要使用网络,所以在Android也统称为Network方式。

(3.1)而WIFI定位与基站定位的结合,

笔者也在网上找到一个很好的文章,笔者对此就不做任何解释,直接给出网址: http://www.cnblogs.com/coffeegg/archive/2011/10/01/2197129.html

(4)AGPS定位:

AGPS(AssistedGPS:辅助全球卫星定位系统)是结合GSM或GPRS与传统卫星定位,利用基地台代送辅助卫星信息,以缩减GPS芯片获取卫星信号的延迟时间,受遮盖的室内也能借基地台讯号弥补,减轻GPS芯片对卫星的依赖度。和纯GPS、基地台三角定位比较,AGPS能提供范围更广、更省电、速度更快的定位服务,理想误差范围在10公尺以内,日本和美国都已经成熟运用AGPS于LBS服务(Location Based Service,基于位置的服务)。AGPS技术是一种结合了网络基站信息和GPS信息对移动台进行定位的技术,可以在GSM/GPRS、WCDMA和CDMA2000网络中使进行用。该技术需要在手机内增加GPS接收机模块,并改造手机的天线,同时要在移动网络上加建位置服务器、差分GPS基准站等设备。AGPS解决方案的优势主要体现在其定位精度上,在室外等空旷地区,其精度在正常的GPS工作环境下,可以达到10米左右,堪称目前定位精度最高的一种定位技术。该技术的另一优点为:首次捕获GPS信号的时间一般仅需几秒,不像GPS的首次捕获时间可能要2~3分钟

感谢原文作者:http://www.cnblogs.com/cuihongyu3503319/p/3863867.html