美文网首页
地理定位

地理定位

作者: 豹发户 | 来源:发表于2017-09-26 10:59 被阅读0次


<script src="/public/home/js/cityPicker.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=7475dff8cd8d5a3bb866baff40b59cde"></script>
<div id="allmap" style="width:0%;height:0px;display: none;"></div>
<div id="container" style="width:0%;height:0px;display: none;"></div>
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=1d3c3f9c0847b35cfde1f1f25529470d"></script>
$(function(){
var map, geolocation;
//加载地图,调用浏览器定位服务
map = new AMap.Map('container', {
resizeEnable: true
});
map.plugin('AMap.Geolocation', function() {
geolocation = new AMap.Geolocation({
enableHighAccuracy: false,
extensions:'all',
convert:true
});
map.addControl(geolocation);
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, 'complete', geolocationResult);//返回定位信息
AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息
});

    //解析定位结果
    function onComplete(data) {
        var userLng = data.position.getLng();
        var userLat = data.position.getLat();
        alert(userLng+","+ userLat);
        get_location(userLat,userLng);
        console.log(data);
    }
    function geolocationResult(data){
        var info = data.pois[0].name
        console.log(data.pois[0]);
        $("#l1").val(data.pois[0].location.lng);
        $("#address2").val(data.pois[0].address);
        $("#j1").val(data.pois[0].location.lat);
        $("#first_address").val(info);
    }
    //解析定位错误信息
    function onError(data) {
        alert('定位失败');
    }

});

$(function () {
// 地图选点还是手动输入
$(".map_tap").click(function(){
$("#xuandian").show();
$("#shuru").hide();
});
$(".hand_type").click(function(){
$("#xuandian").hide();
$("#shuru").show();
$("#map_dis").css('display','none');
});
$('#address_input').bind('input propertychange', function() {
// 百度地图API功能
var map = new BMap.Map("allmap");
map.centerAndZoom(new BMap.Point($("#l1").val(),$("#j1").val()), 12);
var options = {
onSearchComplete: function(results){
// 判断状态是否正确
if (local.getStatus() == BMAP_STATUS_SUCCESS){
var html = '';
for (var i = 0; i < results.getCurrentNumPois(); i ++){
html +='<div class="form_phone">';
html+='<span style="cursor: pointer;" '+' data_lng="'+results.getPoi(i).point.lng+'"'+' data_lat="'+results.getPoi(i).point.lat+'"'+' class="select_address_">'+results.getPoi(i).title+'</span>';

                        html +='</div>';
                        //console.log(results.getPoi(i));
                        $("#address_select").html('');
                        $("#address_select").append(html);
                    }
                }
            }
        };
        var local = new BMap.LocalSearch(map, options);
        local.search($(this).val());
    });
});

相关文章

网友评论

      本文标题:地理定位

      本文链接:https://www.haomeiwen.com/subject/oscsextx.html