主流地图api(包括海外地图)的分析以及使用

您所在的位置:网站首页 为什么高德地图无法在外国使用 主流地图api(包括海外地图)的分析以及使用

主流地图api(包括海外地图)的分析以及使用

2024-07-12 18:54| 来源: 网络整理| 查看: 265

背景

在网站或者手机应用中,经常用到地图api。现在市场不断的涌现各种地图,国内市场当然可以随意使用,除了Google地图之外!因为在一个伟大而神奇的国度,是不允许Google肆意妄为的。尽管如此也不能否认Google地图在国际地图市场的统治力和影响力!

问题

在最近的项目需求分析中,将会用到国外地图、地位以及路线检索的功能。产品设计出来之前,我们需要做的事就是调研和分析-哪些地图在国外好用呢?如何集成?收费情况呢?等等…

调研分析 高德地图API–海外版

目前海外服务提供了地图、定位、POI搜索(包括关键字搜索,周边搜索和POI详情查询)、地理编码(地理编码和逆地理编码)、驾车路线规划和输入提示,共6项服务。 申请试用需要通过工单系统详细描述以下信息:

申请服务:(如,地图、定位、POI搜索、逆地理编码、驾车路线规划)申请平台:(如,Android、iOS、服务端)提供对应的新申请的平台高德Key;

请注意需要全新申请key进行测试,可通过http://lbs.amap.com/dev/key/app注册。例如,开通Android平台地图展示功能,注册Android平台Key即可。 然后就是提交申请,申请测试用量以及开发文档 提交申请

百度地图

由于坐标系统不同直接放弃使用百度地图,有兴趣的同学可以了解和使用下。 申请提示:使用境外路线规划、地点检索、地点输入提示、逆地理编码服务前需要申请开通服务使用权限,其他服务如境外定位、地图、轨迹、步/骑行导航、时区服务请直接访问相关页面官网文档即可。 请确保已成为百度地图认证开发者,再进行境外服务权限申请 申请地址

Google地图

Google作为世界上最好的地图服务提供商,api文档非常详细,当然对国内的支持总不如国内服务商来的实在。开发者网站也经常出现无法访问的情况(你懂的),vpn或者翻墙浏览总不是一件惬意的事情。 首先需要申请一个Google的APIKey,也就是调用地图的凭证;

创建项目地址创建凭据api

这里就只介绍这么多,本次还调研使用了很多地图api做对比就不一一介绍了。有兴趣的同学可以去自己找找度娘,这里提供一些本次加入对比行列的其他地图api

微软bing地图soso地图(腾讯旗下)图吧地图(人人、58、网易均使用的此地图api)搜狗地图 对比

以下对比均属于个人观念,对所有api功能的认知也均停留在2019.9.20之前的版本;可能后续更新也会越做越好的!

高德地图Google地图图吧地图国内目前相对最为流行和常用的地图之一、1)GCJ-02坐标系 AMap就是高德地图,是高德地图在纳斯达克上市用的名字,主要面向互联网企业或个人提供免费API服务毋庸置疑是最大的地图服务提供商,但是在国内不予认可和使用,支持海外地图、定位、检索等;收费情况参考:Google地图方案与价格api的丰富程度及易用性都还可以,可惜的是没有开发地图偏移的转换接口,GPS坐标到火星坐标的转换需要升级为高级用户api。暂无海外版本api

这里我再整理下目前地图常用的坐标系

WGS-84坐标系:地心坐标系,GPS原始坐标体系;在中国,任何一个地图产品都不允许使用GPS坐标,据说是为了保密。(可能是防外星人吧~还有每个国家的秘密基地)

GCJ-02 坐标系:国测局坐标,火星坐标系

1)国测局02年发布的坐标体系,它是一种对经纬度数据的加密算法,即加入随机的偏差。

2)互联网地图在国内必须至少使用GCJ-02进行首次加密,不允许直接使用WGS-84坐标下的地理数据,同时任何坐标系均不可转换为WGS-84坐标。

3)是国内最广泛使用的坐标体系,高德、腾讯、Google中国地图都使用它。

CGCS2000坐标系:国家大地坐标系;该坐标系是通过中国GPS 连续运行基准站、 空间大地控制网以及天文大地网与空间地网联合平差建立的地心大地坐标系统。

BD-09坐标系;百度中国地图所采用的坐标系,由GCJ-02进行进一步的偏移算法得到。

Google api介绍以及使用 web -> Maps JavaScript API 这里有对应的官方文档,我这里简单介绍下使用和使用过程中遇到的问题

这里直接介绍定位的方法,官网也有,针对没有翻墙的小伙伴们,在这里再复制一遍出来,方便大家阅读和使用 – 直接拿定位来举例

Geolocation /* Always set the map height explicitly to define the size of the div * element that contains the map. */ #map { height: 100%; } /* Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; } // Note: This example requires that you consent to location sharing when // prompted by your browser. If you see the error "The Geolocation service // failed.", it means you probably did not give permission for the browser to // locate you. var map, infoWindow; function initMap() { map = new google.maps.Map(document.getElementById('map'), { center: {lat: -34.397, lng: 150.644}, zoom: 6 }); infoWindow = new google.maps.InfoWindow; // Try HTML5 geolocation. if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { var pos = { lat: position.coords.latitude, lng: position.coords.longitude }; infoWindow.setPosition(pos); infoWindow.setContent('Location found.'); infoWindow.open(map); map.setCenter(pos); }, function() { handleLocationError(true, infoWindow, map.getCenter()); }); } else { // Browser doesn't support Geolocation handleLocationError(false, infoWindow, map.getCenter()); } } function handleLocationError(browserHasGeolocation, infoWindow, pos) { infoWindow.setPosition(pos); infoWindow.setContent(browserHasGeolocation ? 'Error: The Geolocation service failed.' : 'Error: Your browser doesn\'t support geolocation.'); infoWindow.open(map); }

这里也做个简单的说明介绍,我们将这个js放在代码中运行发现,位置可能会有偏移更有甚者直接就定位失败,官方也给处理合理的解释

W3C Geolocation standard Applications that want to perform geolocation must support the W3C Geolocation standard. Notice that the sample code above determines the user's location through the W3C navigator.geolocation property. Some browsers use IP addresses to detect a user's location. However, it may only provide a rough estimate of a user's location. The W3C approach is the easiest and most fully-supported so it should be prioritized over other geolocation methods.

大概意思就是说: 想要执行地理定位的应用程序必须支持W3C Geolocation标准。 请注意,上面的示例代码通过W3C navigator.geolocation属性确定用户的位置。某些浏览器使用IP地址来检测用户的位置。 但是,它可能只提供用户位置的粗略估计。 W3C方法是最简单和最全面支持的方法,因此它应优先于其他地理定位方法。

运行代码-出现的错误在这里插入图片描述调试/查看报错信息在这里插入图片描述问题分析-处理方法 报错信息-URL not to be authorized;原因是因为在生成apikey时没有设置访问权限,需要设置一个或者多个访问权限,大概意思就是允许谁来访问这个地图api;加上权限访问后出现新的问题: 在这里插入图片描述

经过多方资料查找,原因竟然是没有绑定账户,Google为了防止使用api过载需要进行银行卡绑定(信用卡),亲测国内常用的visa卡不能绑定成功,必须使用双币卡才行!而且在此吐槽一下各大自动生成信用卡信息的网站~并没有卵用!!!最后还是用的自己的信用卡绑定并扣除1美金认证审核费用!!心疼一年~

OK!经过一些弯路之后终于成功搞定Google地图api的调用(前提是翻墙)! 以上是介绍web-JS的Googleapi调用问题;下面我们再来说说移动端开发的坑一些小问题

iOS开发Google地图

这里我就不具体介绍怎么集成使用了 官方文档里那是相当详细了~这里简单说下遇到的问题 直接上代码

// // ViewController.m // googleDemo // // Created by 王杰 on 2019/9/18. // Copyright © 2019 王杰. All rights reserved. // #import "ViewController.h" #import #import @interface ViewController () @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @property (weak, nonatomic) IBOutlet UILabel *addressLabel; @property (strong, nonatomic) CLLocationManager *locationManager; @end @implementation ViewController{ GMSPlacesClient *_placesClient; GMSCameraPosition *camera; GMSMapView *mapView; GMSMarker *marker; } -(CLLocationManager *)locationManager{ if (!_locationManager) { _locationManager = [[CLLocationManager alloc] init]; _locationManager.desiredAccuracy=kCLLocationAccuracyBest; _locationManager.distanceFilter=200; _locationManager.delegate = self; } return _locationManager; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. if (CLLocationManager.authorizationStatus == kCLAuthorizationStatusNotDetermined) { [self.locationManager requestWhenInUseAuthorization]; // [self.locationManager requestAlwaysAuthorization]; } [self.locationManager startUpdatingLocation];//开启定位 CLLocationCoordinate2D coor = self.locationManager.location.coordinate; marker.position = CLLocationCoordinate2DMake(coor.latitude,coor.longitude); _placesClient = [GMSPlacesClient sharedClient]; // [self.locationManager requestAlwaysAuthorization]; // GMSPlaceField fields = (GMSPlaceFieldName | GMSPlaceFieldPlaceID); // [_placesClient findPlaceLikelihoodsFromCurrentLocationWithPlaceFields:fields callback:^(NSArray * _Nullable likelihoods, NSError * _Nullable error) { // if (error != nil) { // NSLog(@"An error occurred %@", [error localizedDescription]); // return; // } // if (likelihoods != nil) { // for (GMSPlaceLikelihood *likelihood in likelihoods) { // GMSPlace *place = likelihood.place; // NSLog(@"Current place name: %@", place.name); // NSLog(@"Place ID: %@", place.placeID); // } // } // }]; camera = [GMSCameraPosition cameraWithLatitude:coor.latitude longitude:coor.longitude zoom:5]; mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; mapView.myLocationEnabled = YES; mapView.frame = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 500); [self.view addSubview:mapView]; [self.view sendSubviewToBack:mapView]; // Creates a marker in the center of the map. marker = [[GMSMarker alloc] init]; marker.position = coor; marker.title = @"当前位置"; marker.snippet = @"Australia"; marker.map = mapView; } -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocationCoordinate2D coor = manager.location.coordinate; coor = CLLocationCoordinate2DMake(15.898372, 101.604046); marker.position = coor; camera = [GMSCameraPosition cameraWithLatitude:coor.latitude longitude:coor.longitude zoom:camera.zoom]; mapView.camera = camera; } -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ NSLog(@"456"); } - (IBAction)getCurrentPlace:(UIButton *)sender { if (CLLocationManager.authorizationStatus == kCLAuthorizationStatusNotDetermined) { [self.locationManager requestWhenInUseAuthorization]; // [self.locationManager requestAlwaysAuthorization]; } [_placesClient currentPlaceWithCallback:^(GMSPlaceLikelihoodList * _Nullable likelihoodList, NSError * _Nullable error) { if (error != nil) { NSLog(@"Current Place error %@", [error localizedDescription]); return; } self.nameLabel.text = @"No current place"; self.addressLabel.text = @""; if (likelihoodList != nil) { GMSPlace *place = [[[likelihoodList likelihoods] firstObject] place]; if (place != nil) { self.nameLabel.text = place.name; self.addressLabel.text = [[place.formattedAddress componentsSeparatedByString:@", "] componentsJoinedByString:@"\n"]; } } }]; } @end

GooglePlaces – 位置获取不好用!直接采用苹果自带的定位获取当前位置,在用GMSMarker绘画标记位置信息,有兴趣的朋友可以关注下,有问题可以随时评论一起讨论,后续将记录使用过程中遇到的问题



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3