最近公司项目需要做一个类似微信朋友圈的互动交友功能,需要显示用户位置信息,因此在网上查了部分资料,记下demo供以后查看学习:(用到了百度api来实现定位功能)
地理位置测试 gpsPoint; var baiduPoint; var baiduAddress; function getLocation() { (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showMap, handleError, { enableHighAccuracy: }); } else { alert(); } } function showMap(value) { value.coords.longitude; value.coords.latitude; gpsPoint = new BMap.Point(longitude, latitude); BMap.Convertor.translate(gpsPoint, 0, translateCallback); } translateCallback = function (point) { baiduPoint = point; BMap.Geocoder(); geoc.getLocation(baiduPoint, getCityByBaiduCoordinate); } function getCityByBaiduCoordinate(rs) { baiduAddress = rs.addressComponents; baiduAddress.streetNumber; $().html(address); } function handleError(value) { switch (value.code) { case 1: alert(); break; case 2: alert(); break; case 3: alert(); break; case 4: alert(); break; } } function init() { getLocation(); } window.onload = init;