地理定位







$(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 +='

';
html+=''+results.getPoi(i).title+'';

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

你可能感兴趣的:(地理定位)