|
本帖最后由 wyz191 于 2014-7-22 08:36 编辑
- <script type="text/javascript">
- document.addEventListener('deviceready',onDeviceReady,false);
- function onDeviceReady(){
- getLocation();
- }
- function getLocation()
- {
- try{
- if(navigator.geolocation){
- navigator.geolocation.getCurrentPosition(showMap, handleError, {enableHighAccuracy:true, maximumAge: 3000, timeout: 5000});
- }else{
- alert("您的浏览器不支持使用HTML 5来获取地理位置服务");
- }
- }catch(err){
- alert(err);
- }
- }
- function showMap(value){//地图初始化
- try{
- var longitude = value.coords.longitude;
- var latitude = value.coords.latitude;
-
- alert('GPS : '+longitude+" -- "+latitude);
- var map = new BMap.Map("map");
- var point = new BMap.Point(longitude, latitude); // 创建点坐标
- map.centerAndZoom(point, 15);
- var labelgps = new BMap.Label("我是GPS标注哦",{offset:new BMap.Size(20,-10)});
- var marker = new BMap.Marker(new BMap.Point(longitude, latitude)); // 创建标注
- marker.setLabel(labelgps); //添加GPS标注
- map.addOverlay(marker);
- map.addControl(new BMap.NavigationControl());
- //坐标转换完之后的回调函数
- translateCallback = function (point){
- var marker = new BMap.Marker(point);
- map.addOverlay(marker);
- var label = new BMap.Label("我是百度标注哦",{offset:new BMap.Size(20,-10)});
- marker.setLabel(label); //添加百度label
- map.setCenter(point);
- alert(point.lng + "," + point.lat);
- var myGeo = new BMap.Geocoder();
- // 根据坐标得到地址描述
- myGeo.getLocation(point, function(result){
- if (result){
- var province = result.addressComponents.province ;
- var city = result.addressComponents.city;
- var district = result.addressComponents.district ;
- var street = result.addressComponents.street ;
- var streetNumber = result.addressComponents.streetNumber ;
- alert(province + ' - ' +city + ' - ' +district+ ' - ' +street + ' - ' + streetNumber);
- }
- });
- }
- setTimeout(function(){
- BMap.Convertor.translate(point,0,translateCallback); //真实经纬度转成百度坐标
- }, 2000);
- }catch(err){
- alert(err);
- }
- }
- function handleError(value)
- {
- alert('handleError:'+value);
- switch(value.code){
- case 1:
- console.log("位置服务被拒绝");
- break;
- case 2:
- console.log("暂时获取不到位置信息");
- break;
- case 3:
- console.log("获取信息超时");
- break;
- case 4:
- alert("未知错误");
- console.log("未知错误");
- break;
- }
- }
- </script>
复制代码
|
|