h5获取经纬度;然后点击标记marker宣示当前位置

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <title>反地址解析</title>
    <style type="text/css">
        *{
            margin:0px;
            padding:0px;
        }
        body, button, input, select, textarea {
            font: 12px/16px Verdana, Helvetica, Arial, sans-serif;
        }
        p{
            width:603px;
            padding-top:3px;
            margin-top:10px;
            overflow:hidden;
        }
        #container {
            min-width:603px;
            min-height:767px;
        }
    </style>
    <script charset="utf-8" src="http://map.qq.com/api/js?v=2.exp"></script>
    <script>
        var geocoder,map, marker = null;
        var init = function() {
            var center = new qq.maps.LatLng(39.916527,116.397128);
            map = new qq.maps.Map(document.getElementById('container'),{
                center: center,
                zoom: 13
            });
            var info = new qq.maps.InfoWindow({map: map});
            geocoder = new qq.maps.Geocoder({
                complete : function(result){
                    map.setCenter(result.detail.location);
                    var marker = new qq.maps.Marker({
                        map:map,
                        position: result.detail.location
                    });
                    //添加监听事件 当标记被点击了  设置图层
                    qq.maps.event.addListener(marker, 'click', function() {
                        info.open();
                        info.setContent('<div style="width:280px;height:100px;">'+
                            result.detail.address+'</div>');
                        info.setPosition(result.detail.location);
                    });
                }
            });
        }

        function getLocation(){

            if (navigator.geolocation){

                navigator.geolocation.getCurrentPosition(showPosition);

            }else{
                x.innerHTML="Geolocation is not supported by this browser.";
            }
        }
        function showPosition(position){

            var lat=position.coords.latitude;
            var lng=position.coords.longitude;
            console.log(lat)
            console.log(lng)
            var latLng = new qq.maps.LatLng(lat, lng);
            //调用信息窗口
            var info = new qq.maps.InfoWindow({map: map});
            //调用获取位置方法
            geocoder.getAddress(latLng);
        }

    </script>
</head>
<body onload="init()">
<div>
    <input id="latLng" type="textbox" value="39.98174,116.30631">
    <input type="button" value="search" onclick="getLocation()">
</div>
<div id="container"></div>
<p>输入坐标,点击search进行反地址解释,点击marker会弹出反查结果。</p>
</body>
</html>

现在的问题是无法获取经纬度;
console.log(lat)console.log(lng) 看不到经纬度

阅读 4.5k
3 个回答

手机上应该可以的,电脑还是用ip定位吧

链接描述

谢邀,这个得查看地图API有没有提供根据经纬度获取位置的接口,一般猜想应该是有的。然后看看自己写的代码有没有问题。主要还是看下地图提供的API。看你问题说是无法获取经纬度,你是根据什么来获取经纬度的?你的坐标是什么形式。符合API的调用条件吗?

<script charset="utf-8" src="http://map.qq.com/api/js?v=2.exp"></script>是不是少个ak=***
我知道百度的是需要的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题