可以看出,变量distance是计算得到的两地的距离,而results[0]被初始为distance,通过对results数组所含值的个数做判断,当为大于等于2时,results[1]存放的是initial bearing,当为大于等于3时,results[2]存放的是final bearing。由此我们也可以知道,GPS定位获取的方位不是像指南针那样原地指明方向,而是得出的是你运动的方向。
JNI层主要是连接Framework和HAL,调用HAL层具体硬件抽象的实现。
HAL(Hardware Abstract Layer),硬件抽象层。我们知道Android底层是基于Linux的,HAL就相当于Linux应用程序接口,从而进行硬件设备操作。hardware\libhardware\include\hardware中有gps.h文件,从中可以看到使用的数据结构。具体的实现是由高通提供的,在hardware\qcom\gps中。
主要看一下gps.h文件。下面是GpsInterface,是底层驱动实现的接口。当其中的接口被调用时,可以实现相应的操作,注释已经比较完整。感觉比较特殊的是int (*inject_time)(GpsUtcTime time, int64_t timeReference,int uncertainty),可以在两种情况下被调用,分别是应用发送相应命令时调用和网络可获取时自动调用。
/** Represents the standard GPS interface. */
typedef struct {
/** set to sizeof(GpsInterface) */
size_t size;
/**
* Opens the interface and provides the callback routines
* to the implemenation of this interface.
*/
int (*init)( GpsCallbacks* callbacks );
/** Starts navigating. */
int (*start)( void );
/** Stops navigating. */
int (*stop)( void );
/** Closes the interface. */
void (*cleanup)( void );
/** Injects the current time. */
int (*inject_time)(GpsUtcTime time, int64_t timeReference,
int uncertainty);
/** Injects current location from another location provider
* (typically cell ID).
* latitude and longitude are measured in degrees
* expected accuracy is measured in meters
*/
int (*inject_location)(double latitude, double longitude, float accuracy);
/**
* Specifies that the next call to start will not use the
* information defined in the flags. GPS_DELETE_ALL is passed for
* a cold start.
*/
void (*delete_aiding_data)(GpsAidingData flags);
/**
* min_interval represents the time between fixes in milliseconds.
* preferred_accuracy represents the requested fix accuracy in meters.
* preferred_time represents the requested time to first fix in milliseconds.
*/
int (*set_position_mode)(GpsPositionMode mode, GpsPositionRecurrence recurrence,
uint32_t min_interval, uint32_t preferred_accuracy, uint32_t preferred_time);
/** Get a pointer to extension information. */
const void* (*get_extension)(const char* name);
} GpsInterface;
下面是GpsLocation,可以看出Location的包含内容。主要有经度、维度、方向、海拔、速度、使用的准确度、时间。底层驱动会获取Location的信息后通过相应解析得到所需要的信息。
/** Represents a location. */
typedef struct {
/** set to sizeof(GpsLocation) */
size_t size;
/** Contains GpsLocationFlags bits. */
uint16_t flags;
/** Represents latitude in degrees. */
double latitude;
/** Represents longitude in degrees. */
double longitude;
/** Represents altitude in meters above the WGS 84 reference
* ellipsoid. */
double altitude;
/** Represents speed in meters per second. */
float speed;
/** Represents heading in degrees. */
float bearing;
/** Represents expected accuracy in meters. */
float accuracy;
/** Timestamp for the location fix. */
GpsUtcTime timestamp;
} GpsLocation;