商铺定位开发简单记录

坐标体系

  1. WGS-84:是美国国防部对地理空间全球参考系统的标准定义,即全球定位系统使用的参考坐标系。
  2. GCJ-02,国测局02年发布的坐标体系。又称“火星坐标”。比如谷歌,腾讯,高德都在用这个坐标体系
  3. 其他坐标体系。例如百度和搜狗就使用自己的坐标体系,与其他坐标体系不兼容

各地图坐标

在各种 web 端平台,或者高德、腾讯、百度上取到的坐标,都不是 GPS 坐标,都是 GCJ-02 坐标,或者自己的偏移坐标系。
比如,你在谷歌地图API,高德地图API,腾讯地图API上取到的,都是 GCJ-02 坐标,他们三家都是通用的,也适用于大部分地图 API 产品,以及他们的地图产品。
例外,百度 API 上取到的,是 BD-09 坐标,只适用于百度地图相关产品。
例外,搜狗 API 上取到的,是搜狗坐标,只适用于搜狗地图相关产品。
例外,谷歌地球,google earth上取到的,是GPS坐标,而且是度分秒形式的经纬度坐标。在国内不允许使用。必须转换为 GCJ-02 坐标。

定位技术

定位技术大概有如下几种

  • 基站定位
  • wifi定位
  • IP定位
  • GPS定位
  • AGPS定位

H5开发中可以用到的定位

  • 微信JSSDK定位
  • HTML5定位
  • 高德等地图API定位
  • 目前开发的定位方式为,在微信容器内,采用微信定位,其它采用高德地图api定位,高德地图融合了H5浏览器定位、IP定位。

下面是开发的代码,以备后期回顾使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
let context = {defaultCity: '上海'};

$(function(){
locate_local_city();
$('a').addClass('external');
});

/**
* 根据定位加载门店列表
*
*/
function locate_local_city() {
//微信内用未定定位
if (in_wx_env) {
//获取火星坐标,用来去获取城市
wx.ready(function () {
wx.getLocation({
type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
success: function (res) {
// location_lat = res.latitude; // 纬度,浮点数,范围为90 ~ -90
// location_lng = res.longitude; // 经度,浮点数,范围为180 ~ -180。
console.log('微信获取gcj02坐标;');
gdMapGetPositionByCoordinate(res.longitude, res.latitude);
},
});
});
//延时执行微信定位,因之前用百度定位的城市不准确,需要用微信获取gcj02坐标通过高德获取城市名
setTimeout("wxGeoLocation()", 2000);
} else {
gdMapGeoLocation();
}
}

/**
* 设置顶部选择器中的城市
* @param city
*/
function setSelectorCity(city) {
let addr = city.replace('市', '');
$('.item-addr span').text(addr);
}

//微信定位
function wxGeoLocation() {
wx.ready(function () {
wx.getLocation({
type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
success: function (res) {
location_lat = res.latitude; // 纬度,浮点数,范围为90 ~ -90
location_lng = res.longitude; // 经度,浮点数,范围为180 ~ -180。
afterGeoLocation();
console.log('微信定位成功;');
},
fail: function () {
console.log('微信定位失败;');
}
});
});
}

//高德地图定位
function gdMapGeoLocation()
{
mapObj = new AMap.Map('iCenter');
mapObj.plugin('AMap.Geolocation', function () {
geolocation = new AMap.Geolocation({
enableHighAccuracy: true,//是否使用高精度定位,默认:true
GeoLocationFirst: true, //设置为true的时候可以调整PC端为优先使用浏览器定位,失败后使用IP定位
timeout: 10000, //超过10秒后停止定位,默认:无穷大
maximumAge: 0, //定位结果缓存0毫秒,默认:0
convert: false, //自动偏移坐标,偏移后的坐标为高德坐标,默认:true
panToLocation: true, //定位成功后将定位到的位置作为地图中心点,默认:true
// noIpLocate: 1, //是否开启IP定位
});
geolocation.getCurrentPosition(function (status, result) {
if (status == 'complete') {
location_lng = result.position.lng;
location_lat = result.position.lat;
locale_region_name = result.addressComponent.province;
gdMapGetPositionByCoordinate(location_lng, location_lat);
setSelectorCity(locale_region_name);
afterGeoLocation();
} else {
console.log('高德地图定位:' + status);
locale_region_name = context.defaultCity;
setSelectorCity(context.defaultCity);
afterGeoLocation();
}
console.log(result);
});
});
}

/**
* 完成定位后的动作
*/
function afterGeoLocation() {
init_show_map(locale_region_name);
nearest_shop_list(1);
}

//根据微信获取的gcj02经纬度获取城市
function gdMapGetPositionByCoordinate(lng, lat) {
// let mapObj = new AMap.Map('iCenter');
AMap.service('AMap.Geocoder', function () {
geocoder = new AMap.Geocoder({});
geocoder.getAddress([lng, lat], function (status, result) {
if (status === 'complete') {
locale_region_name = result.regeocode.addressComponent.province;
setSelectorCity(locale_region_name);
}
});
});
}

// //百度地图定位
// function bdMapGeoLocation()
// {
// var geolocation = new BMap.Geolocation();
// geolocation.getCurrentPosition(function(r){
// if(this.getStatus() == BMAP_STATUS_SUCCESS){
// // let mk = new BMap.Marker(r.point);
// // map.addOverlay(mk);
// // map.panTo(r.point);
// console.log('百度定位您的坐标:' + r.point.lng + ',' + r.point.lat);
// let wgs_84 = bdCoordinateConvertWGS84(r.point.lng, r.point.lat);
// console.log('百度转wgs-84坐标:' + wgs_84.lng + ',' + wgs_84.lat);
// location_lat = wgs_84.lat;
// location_lng = wgs_84.lng;
// init_show_map(locale_region_name);
// nearest_shop_list(1);
// } else {
// console.log('failed'+this.getStatus());
// }
// },{enableHighAccuracy: true})
// }
//
// //百度坐标转wgs84
// function bdCoordinateConvertWGS84(lng, lat)
// {
// //百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
// let gcj_02 = bd09togcj02(lng, lat);
// let wgs_84 = gcj02towgs84(gcj_02[0], gcj_02[1]);
// return {lng:wgs_84[0], lat:wgs_84[1]};
// }

//--------------------------------------这一坨是一起的------------------END----------------------------------------------------------------------
Author: rexmolo
Link: http://rexmolo.github.io/2019/04/06/geo-location-develop/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.