
$(document).ready(function(){
    // 获取IP地址
    var map = new BMap.Map("allmap");
    var HeightMapHtml = $('#HeightMapHtml');
    $.ajax({
            type: "get",
            async: false,
            url: '/userip.php?userIP=nowIP',
            dataType: 'json',
            success: function(data){
                if (data['status'] == 0) {
                    $('.UserIp').val(data['info']);
                    userMAP(data['info']);
                } else {
                    $('.UserIp').val(data['info']);
                    capital();
                };
             },
            error: function(){
                capital();
                HeightMapHtml.html('服务忙，请稍候再试！');
                HeightMapHtml.css('color', '#f00');
             }
        });
    // 默认显示自己的位置
    function userMAP(UserIp) {
        var NowUrl = 'http://api.map.baidu.com/highacciploc/v1?ak=D5xRa5QyGjb0NzwEBYk9wQaEpXyFOat9&qterm=pc&callback_type=jsonp&coord=bd09ll&qcip=' + UserIp;
        $.ajax({
            type: "get",
            async: false,
            url: NowUrl,
            dataType: "jsonp",
            success: function(data){
                var result = data.result.error;
                if (result == 161) {
                    showMap(data);
                } else {
                    capital();
                };
             },
            error: function(){
                capital();
                HeightMapHtml.html('服务忙，请稍候再试！');
                HeightMapHtml.css('color', '#f00');
             }
        });
    }
    // 百度地图API功能

    // 定位失败显示天安门地图
    function capital() {
        map.clearOverlays();
        map.centerAndZoom(new BMap.Point(116.404, 39.915),14);
        map.enableScrollWheelZoom(true);   // 创建标注        
    }
    // 点击运行
    $('#HeightMapBtn').click(function () {
        var mapUrl = 'http://api.map.baidu.com/highacciploc/v1?ak=D5xRa5QyGjb0NzwEBYk9wQaEpXyFOat9&qterm=pc&callback_type=jsonp&coord=bd09ll&qcip=' + $('.UserIp').val();

        $.ajax({
            type: "get",
            async: false,
            url: mapUrl,
            dataType: "jsonp",
            success: function(data){
                var result = data.result.error;
                if (result == 161) {
                    showMap(data);
                } else {
                    HeightMapHtml.html('定位失败！&nbsp;&nbsp;' + result);
                    HeightMapHtml.css('color', '#f00');
                };
             },
            error: function(){
                capital();
                HeightMapHtml.html('服务忙，请稍候再试！');
                HeightMapHtml.css('color', '#f00');
             }
        });

    });
    // 显示地图公共函数
    function showMap (data){
        map.clearOverlays();
        map.centerAndZoom(new BMap.Point(data.content.location.lng, data.content.location.lat),14);
        var point = new BMap.Point(data.content.location.lng, data.content.location.lat);
        var marker = new BMap.Marker(point);
        map.enableScrollWheelZoom(true);   // 创建标注
        map.addOverlay(marker);
        HeightMapHtml.html('经度：' + data.content.location.lng + '&nbsp;&nbsp;&nbsp;&nbsp;纬度：' + data.content.location.lat);
        HeightMapHtml.css('color', '#626262');
    }

});