源码分析:

    https://www.ibm.com/developerworks/cn/opensource/os-cn-android-location/

    

 1. 源码所在的目录

2. 

http://ju.outofmemory.cn/entry/111182

3. 

4. LocationManager生成实例的位置

5. frameworks/base/core/java/android/app/ContextImpl.java


registerService(LOCATION_SERVICE, new ServiceFetcher() {
    public Object createService(ContextImpl ctx) {
        IBinder b = ServiceManager.getService(LOCATION_SERVICE);
        return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
    }
});

       而ServiceManager里的的"LOCATION_SERVICE"又是从com.android.server.SystemServer这个类添加进去的

if (!disableLocation) {
    traceBeginAndSlog("StartLocationManagerService");
    try {
        location = new LocationManagerService(context);
        ServiceManager.addService(Context.LOCATION_SERVICE, location);
    } catch (Throwable e) {
        reportWtf("starting Location Manager", e);
    }
    Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

    traceBeginAndSlog("StartCountryDetectorService");
    try {
        countryDetector = new CountryDetectorService(context);
        ServiceManager.addService(Context.COUNTRY_DETECTOR, countryDetector);
    } catch (Throwable e) {
        reportWtf("starting Country Detector", e);
    }
    Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}

   LocationManagerService的reportLocation方法触发了应用上层onLocationFound的回调

@Override
public void reportLocation(Location location, boolean passive) {
    checkCallerIsProvider();

    if (!location.isComplete()) {
        Log.w(TAG, "Dropping incomplete location: " + location);
        return;
    }

    mLocationHandler.removeMessages(MSG_LOCATION_CHANGED, location);
    Message m = Message.obtain(mLocationHandler, MSG_LOCATION_CHANGED, location);
    m.arg1 = (passive ? 1 : 0);
    mLocationHandler.sendMessageAtFrontOfQueue(m);
}

    顺藤摸瓜,最终找到

public boolean callLocationChangedLocked(Location location) {
    if (mListener != null) {
        try {
            synchronized (this) {
                // synchronize to ensure incrementPendingBroadcastsLocked()
                // is called before decrementPendingBroadcasts()
                mListener.onLocationChanged(new Location(location));
                // call this after broadcasting so we do not increment
                // if we throw an exeption.
                incrementPendingBroadcastsLocked();
            }
        } catch (RemoteException e) {
            return false;
        }
    } else {

    那么LocationManagerService的reportLocation方法又是怎么被触发的呢?

    

 

    

1. ILocationManager.aidl

2. frameworks/base/location/java/android/location/ILocationManager.aidl

3. 

4. 

  GPS定位漂移产生的原因:https://baike.1688.com/doc/view-d41865087.html

  GPS的NEMA协议解析:https://blog.csdn.net/adazone/article/details/45152291

  点到直线的距离计算公式:https://blog.csdn.net/bagboy_taobao_com/article/details/6291462

  经纬度求坐标方位角: https://blog.csdn.net/u010175314/article/details/76093934

  NMEA编码:https://baike.baidu.com/item/NMEA/9812575?fr=aladdin