我正在学习Google Maps API V2,现在我正在阅读Location API
,文档说明它将client
与Service
相关联:
位置服务通过以下方式将当前位置发送到您的应用 location client,它是Location Services类的一个实例 LocationClient。所有位置信息请求都要经过这一点 客户端。
这是否意味着Google将获得我的应用所在的所有职位?我知道如果已下载地图,Google地图可以在离线模式下工作,但位置服务是否仅在线运行?
我需要控制我的应用中的互联网流量,所以问题就这样了。
答案 0 :(得分:2)
它的工作方式:
这种方式使用您的服务提供商网络连接来确定您的位置。返回的经度和纬度绝对不是最准确的,但是它们可以达到10-15%的准确度(根据我的经验)。
以下是一段代码片段(直接从Android网站上拉)
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
这种方法比前一种方法准确得多。但缺点是您必须在线并连接到互联网。此方法使用与在任何Web浏览器上找到的JavaScript版本的Google地图相同的理论。这种方法返回的经度和纬度几乎是准确的。
GPS是最准确的,它只能在户外工作,很快就会消耗电池电量,并且不会像用户想要的那样快速返回位置。然而,GPS不需要连接到互联网或移动数据。它通过请求公共卫星获取您的位置。
以下是您需要的两个功能。 getLatitude() and getLongitude().
请记住,这一切都很有趣。但谷歌地图api v2有一个简单的function
mMap.setMyLocationEnabled(true)
;
考虑所有三种可能性来获取您的位置。