location — iOS上的地理位置服务

location模块允许你在iOS上访问地理位置数据(例如GPS)。此外,你可以使用location模块将地址转换为坐标,反之亦然(地理编码和反向地理编码)。

注意:首次使用此模块时,将显示系统弹出的权限警报。除非你授予应用访问你的位置的权限,否则大多数功能都不会返回有意义的结果。

location模块中的功能:

location.get_location()

返回最近获取的位置数据。返回值是一个包含各种数字的字典,这些数字描述了当前位置(最重要的是“经度”,“纬度”和“时间戳”)。该字典可用作该reverse_geocode()函数的参数。如果尚未获取位置数据(或你尚未授权该应用程序),则返回值可能为None。

location.start_updates()

开始更新位置数据。应该在使用get_location()之前调用它。为了节省电量,建议你在不再需要位置数据时调用stop_updates()。

location.stop_updates()

停止更新位置数据。

location.geocode(address)

尝试将地址字典转换为地理坐标。返回值是可能坐标的列表(如果未找到结果,则可以为空)。列表中的每个条目都是带有“经度”和“纬度”键的字典。

例:

1

2

3

4import location

address_dict = {'Street': 'Infinite Loop', 'City': 'Cupertino', 'Country': 'USA'}

results = location.geocode(address_dict)

print(results)

location.render_map_snapshot(lat, lng, width=1000, height=1000, map_type=’standard’, show_poi=True, img_width=240, img_height=240, img_scale=0)

渲染/下载给定地图区域的快照图像(使用Apple Maps框架)。

lat和lng参数决定了地图中心的纬度和经度。width和height(以米为单位)间接设置地图的比例因子。map_type可以是’standard‘,’satellite‘或’hybrid‘。

返回值是一个ui.Image具有给定的大小(img_width / img_height)和比例(默认为0,即当前设备的屏幕比例)。

例:

1

2

3import location

img = location.render_map_snapshot(48.8582, 2.2945, map_type='satellite', img_width=512, img_height=512)

img.show()

location.reverse_geocode(location)

尝试将位置(经度/纬度)转换为人类可读的地址。location参数应该是包含关于“经度”和“纬度”值的字典。返回值是可能的地址字典的列表(通常只有一个)。

例:

1

2

3

4import location

coordinates = {'latitude': 37.331684, 'longitude': -122.030758}

results = location.reverse_geocode(coordinates)

print(results)

location.is_authorized()

如果当前允许访问位置数据,则返回True,否则返回False(例如,如果尚未显示权限对话框,或者由于父母控制而拒绝访问)。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐