javascript - h5获取经纬度;然后点击标记marker宣示当前位置
迷茫
迷茫 2017-04-11 11:09:31
[JavaScript讨论组]

!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('<p style="width:280px;height:100px;">'+
                        result.detail.address+'</p>');
                    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()">
<p>

<input id="latLng" type="textbox" value="39.98174,116.30631">
<input type="button" value="search" onclick="getLocation()">

</p>
<p id="container"></p>
<p>输入坐标,点击search进行反地址解释,点击marker会弹出反查结果。</p>
</body>
</html>

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

                      console.log(lng)看不到经纬度
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(3)
PHP中文网

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

链接描述

黄舟

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

PHP中文网

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号