百度地图 通过坐标搜索地址并显示一定范围内的点集合

本文介绍如何使用百度地图API进行点标注、逆地址解析、创建范围圈,并判断其他点是否位于该范围内。通过示例代码,展示了如何实现这些功能,包括获取坐标、创建点标记、逆地理编码获取地址、设定圆的半径、创建圆形范围,以及遍历多个点判断其是否在指定范围内。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述效果图
效果:添加点标注-坐标搜索地址(逆地址解析)-形成一个范围圈-判断其余点是否在圈内

添加标注

  // 获取坐标值
                var latlon = $('.latlon').val().split(',');
                // 添加点标注
                var point = new BMap.Point(latlon[0], latlon[1]);
                marker = new BMap.Marker(point);
                map.addOverlay(marker);

地址解析

// 逆地址解析
            var geoc = new BMap.Geocoder();
            geoc.getLocation(point, function(rs){
                // console.log(rs)
                // 显示地址
                $('.local').html(rs.address)
            });

范围圈

// 获取圆半径
        var r = $('.radius').val() * 1000;
        // 创建以点为中心的圆
        circle = new BMap.Circle(point,r,
            {
                strokeColor:"#e61623", //线颜色
                strokeWeight:2, //线宽
                fillColor:"#f18900", // 填充色
                fillOpacity:'0.1',  // 填充透明度
            });
        map.addOverlay(circle);

判断

// 显示圆范围内的点
        for(var i = 0;i < points.length;i++){
            var result = BMapLib.GeoUtils.isPointInCircle(points[i], circle);
            if (result == true) {
                circleMarker = new BMap.Marker(points[i]);
                map.addOverlay(circleMarker);
            }
        }

完整代码

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
    body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=3.0&ak=您的ak"></script>

    <script type="text/javascript" src="http://api.map.baidu.com/library/GeoUtils/1.2/src/GeoUtils_min.js"></script>
    <title>地图展示</title>
</head>
<body>
    <input type="text" name="" value="125.3333815941,43.9018671664" placeholder="输入坐标,例:125.3333815941,43.9018671664" class="latlon">
    <button type="button" onclick="search()">搜索</button>
    <select name="" class="radius">
        <option value="10">10公里内</option>
        <option value="20">20公里内</option>
    </select>
    当前位置:<span class="local"></span>
    <div id="allmap"></div>
</body>
</html>
<script src="http://www.jq22.com/jquery/jquery-1.8.2.js"></script>
<script type="text/javascript">
    // 百度地图API功能
    var map = new BMap.Map("allmap");    // 创建Map实例
    map.centerAndZoom(new BMap.Point(125.3333815941, 43.9018671664), 12);  // 初始化地图,设置中心点坐标和地图级别
    //添加地图类型控件
    map.addControl(new BMap.MapTypeControl({
        mapTypes:[
            BMAP_NORMAL_MAP,
            BMAP_HYBRID_MAP
        ]}));
    map.setCurrentCity("吉林");          // 设置地图显示的城市 此项是必须设置的
    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
    // 点 坐标
    var points = [
        new BMap.Point(125.2742200750, 43.9148113393),
        new BMap.Point(125.2563399144, 43.9652697507),
        new BMap.Point(125.2055261923, 43.9944885503)
    ];
    var circleMarker,  // 圆内点
        circle,   // 圆
        marker;  // 搜索点
    // 搜索按钮点击
    function search(){
        // 清除所有覆盖物
        map.clearOverlays();
        // 获取坐标值
        var latlon = $('.latlon').val().split(',');
        // 添加点标注
        var point = new BMap.Point(latlon[0], latlon[1]);
        marker = new BMap.Marker(point);
        map.addOverlay(marker);
        // 设置中心点
        map.setCenter(point)
        // 逆地址解析
        var geoc = new BMap.Geocoder();
        geoc.getLocation(point, function(rs){
            // console.log(rs)
            $('.local').html(rs.address)
        });
        // 获取圆半径
        var r = $('.radius').val() * 1000;
        // 创建以点为中心的圆
        circle = new BMap.Circle(point,r,
            {
                strokeColor:"#e61623", //线颜色
                strokeWeight:2, //线宽
                fillColor:"#f18900", // 填充色
                fillOpacity:'0.1',  // 填充透明度
            });
        map.addOverlay(circle);
        // 显示圆范围内的点
        for(var i = 0;i < points.length;i++){
            var result = BMapLib.GeoUtils.isPointInCircle(points[i], circle);
            if (result == true) {
                circleMarker = new BMap.Marker(points[i]);
                map.addOverlay(circleMarker);
            }
        }
    }
    $('.radius').change(function(){
        search()
    });

    // 地图个性化样式
    var styleJson = [{
        "featureType": "land",
        "elementType": "geometry",
        "stylers": {
            "visibility": "on",
            "color": "#e9e9e7ff"
        }
    }, {
        "featureType": "water",
        "elementType": "geometry",
        "stylers": {
            "visibility": "on",
            "color": "#7ab5ddff"
        }
    }, {
        "featureType": "green",
        "elementType": "geometry",
        "stylers": {
            "visibility": "on",
            "color": "#0d50a1ff"
        }
    }, {
        "featureType": "building",
        "elementType": "geometry",
        "stylers": {
            "visibility": "on"
        }
    }, {
        "featureType": "building",
        "elementType": "geometry.fill",
        "stylers": {
            "color": "#ffffffb3"
        }
    }, {
        "featureType": "building",
        "elementType": "geometry.stroke",
        "stylers": {
            "color": "#dadadab3"
        }
    }, {
        "featureType": "subwaystation",
        "elementType": "geometry",
        "stylers": {
            "visibility": "on",
            "color": "#b15454B2"
        }
    }, {
        "featureType": "education",
        "elementType": "geometry",
        "stylers": {
            "visibility": "on",
            "color": "#e4f1f1ff"
        }
    }, {
        "featureType": "medical",
        "elementType": "geometry",
        "stylers": {
            "visibility": "on",
            "color": "#f0dedeff"
        }
    }, {
        "featureType": "scenicspots",
        "elementType": "geometry",
        "stylers": {
            "visibility": "on",
            "color": "#e2efe5ff"
        }
    }, {
        "featureType": "highway",
        "elementType": "geometry",
        "stylers": {
            "visibility": "on",
            "weight": 4
        }
    }, {
        "featureType": "highway",
        "elementType": "geometry.fill",
        "stylers": {
            "color": "#f7c54dff"
        }
    }, {
        "featureType": "highway",
        "elementType": "geometry.stroke",
        "stylers": {
            "color": "#fed669ff"
        }
    }, {
        "featureType": "highway",
        "elementType": "labels",
        "stylers": {
            "visibility": "on"
        }
    }, {
        "featureType": "highway",
        "elementType": "labels.text.fill",
        "stylers": {
            "color": "#8f5a33ff"
        }
    }, {
        "featureType": "highway",
        "elementType": "labels.text.stroke",
        "stylers": {
            "color": "#ffffffff"
        }
    }, {
        "featureType": "highway",
        "elementType": "labels.icon",
        "stylers": {
            "visibility": "on"
        }
    }, {
        "featureType": "arterial",
        "elementType": "geometry",
        "stylers": {
            "visibility": "on",
            "weight": 2
        }
    }, {
        "featureType": "arterial",
        "elementType": "geometry.fill",
        "stylers": {
            "color": "#d8d8d8ff"
        }
    }, {
        "featureType": "arterial",
        "elementType": "geometry.stroke",
        "stylers": {
            "color": "#ffeebbff"
        }
    }, {
        "featureType": "arterial",
        "elementType": "labels",
        "stylers": {
            "visibility": "on"
        }
    }, {
        "featureType": "arterial",
        "elementType": "labels.text.fill",
        "stylers": {
            "color": "#525355ff"
        }
    }, {
        "featureType": "arterial",
        "elementType": "labels.text.stroke",
        "stylers": {
            "color": "#ffffffff"
        }
    }, {
        "featureType": "local",
        "elementType": "geometry",
        "stylers": {
            "visibility": "on",
            "weight": 1
        }
    }, {
        "featureType": "local",
        "elementType": "geometry.fill",
        "stylers": {
            "color": "#d8d8d8ff"
        }
    }, {
        "featureType": "local",
        "elementType": "geometry.stroke",
        "stylers": {
            "color": "#ffffffff"
        }
    }, {
        "featureType": "local",
        "elementType": "labels",
        "stylers": {
            "visibility": "on"
        }
    }, {
        "featureType": "local",
        "elementType": "labels.text.fill",
        "stylers": {
            "color": "#979c9aff"
        }
    }, {
        "featureType": "local",
        "elementType": "labels.text.stroke",
        "stylers": {
            "color": "#ffffffff"
        }
    }, {
        "featureType": "railway",
        "elementType": "geometry",
        "stylers": {
            "visibility": "off",
            "weight": 1
        }
    }, {
        "featureType": "railway",
        "elementType": "geometry.fill",
        "stylers": {
            "color": "#949494ff"
        }
    }, {
        "featureType": "railway",
        "elementType": "geometry.stroke",
        "stylers": {
            "color": "#ffffffff"
        }
    }, {
        "featureType": "subway",
        "elementType": "geometry",
        "stylers": {
            "visibility": "on",
            "weight": 1
        }
    }, {
        "featureType": "subway",
        "elementType": "geometry.fill",
        "stylers": {
            "color": "#d8d8d8ff"
        }
    }, {
        "featureType": "subway",
        "elementType": "geometry.stroke",
        "stylers": {
            "color": "#ffffffff"
        }
    }, {
        "featureType": "subway",
        "elementType": "labels",
        "stylers": {
            "visibility": "on"
        }
    }, {
        "featureType": "subway",
        "elementType": "labels.text.fill",
        "stylers": {
            "color": "#979c9aff"
        }
    }, {
        "featureType": "subway",
        "elementType": "labels.text.stroke",
        "stylers": {
            "color": "#ffffffff"
        }
    }, {
        "featureType": "continent",
        "elementType": "labels",
        "stylers": {
            "visibility": "on"
        }
    }, {
        "featureType": "continent",
        "elementType": "labels.icon",
        "stylers": {
            "visibility": "on"
        }
    }, {
        "featureType": "continent",
        "elementType": "labels.text.fill",
        "stylers": {
            "color": "#333333ff"
        }
    }, {
        "featureType": "continent",
        "elementType": "labels.text.stroke",
        "stylers": {
            "color": "#ffffffff"
        }
    }, {
        "featureType": "city",
        "elementType": "labels.icon",
        "stylers": {
            "visibility": "on"
        }
    }, {
        "featureType": "city",
        "elementType": "labels",
        "stylers": {
            "visibility": "on"
        }
    }, {
        "featureType": "city",
        "elementType": "labels.text.fill",
        "stylers": {
            "color": "#454d50ff"
        }
    }, {
        "featureType": "city",
        "elementType": "labels.text.stroke",
        "stylers": {
            "color": "#ffffffff"
        }
    }, {
        "featureType": "town",
        "elementType": "labels.icon",
        "stylers": {
            "visibility": "on"
        }
    }, {
        "featureType": "town",
        "elementType": "labels",
        "stylers": {
            "visibility": "on"
        }
    }, {
        "featureType": "town",
        "elementType": "labels.text.fill",
        "stylers": {
            "color": "#454d50ff"
        }
    }, {
        "featureType": "town",
        "elementType": "labels.text.stroke",
        "stylers": {
            "color": "#ffffffff"
        }
    }, {
        "featureType": "road",
        "elementType": "geometry.fill",
        "stylers": {
            "color": "#a0c6f3ff"
        }
    }, {
        "featureType": "poilabel",
        "elementType": "labels.icon",
        "stylers": {
            "visibility": "off"
        }
    }, {
        "featureType": "poilabel",
        "elementType": "labels.text.stroke",
        "stylers": {
            "color": "#ffffff0"
        }
    }, {
        "featureType": "poilabel",
        "elementType": "labels.text.fill",
        "stylers": {
            "color": "#428ee9ff"
        }
    }, {
        "featureType": "districtlabel",
        "elementType": "labels",
        "stylers": {
            "visibility": "off"
        }
    }, {
        "featureType": "manmade",
        "elementType": "geometry",
        "stylers": {
            "color": "#7ab5ddff"
        }
    }]
    map.setMapStyleV2({styleJson:styleJson});
</script>
<think>好的,用户想了解如何使用百度地图API根据坐标点数组自动调整视图等级。首先,我需要回忆一下百度地图API的相关方法。记得有一个`Bounds`类用来表示范围,可能需要先根据坐标数组创建这个范围,然后使用`map.setViewport()`方法调整视图。用户提到“自动调整视图等级”,也就是要让地图显示所有给定的坐标点,且自动计算合适的缩放级别。这时候应该用到`map.setViewport(points)`或者类似的函数,传入坐标数组。不过需要确认具体的API版本,不同版本可能有差异。 然后,我需要检查示例代码是否正确。比如,初始化地图时可能需要先设置中心点和默认缩放级别,但之后通过`setViewport`调整。另外,是否需要先创建`Bounds`对象,扩展每个点进去?比如遍历坐标数组,逐个添加到Bounds中,最后设置地图的视野。还要注意是否需要添加导航控件,比如`BMap.NavigationControl`,但用户的问题可能不涉及控件,只是调整视图。不过示例中的代码可能包含这部分,需要确认是否必要。 另外,引用[1]中提到`map.centerAndZoom(points[6],16);`,这里设置的是单个点为中心,但用户需要的是多个点的范围。所以应该替换这部分代码,使用`setViewport`或者计算Bounds后的方法。 可能还需要处理坐标系的转换,但用户的问题没有提到这点,假设坐标已经是百度坐标系。如果有其他坐标系的点,可能需要转换,但问题中用户没有提,暂时忽略。最后,生成相关问题时要考虑用户可能的后续需求,比如性能优化、不同坐标系的处理、添加标注等。确保问题与自动调整视图相关,覆盖常见的使用场景。</think>### 根据坐标点数组自动调整视图等级的实现方法 使用百度地图API实现根据坐标点数组自动调整视图等级,需通过以下步骤: 1. **创建地图实例初始化** 初始化时建议选择坐标数组中的第一个点作为临时中心点,避免地图初始化时无定位: ```javascript var map = new BMap.Map("container"); var points = [new BMap.Point(116.404, 39.915), new BMap.Point(117.500, 40.200)]; //示例坐标数组 map.centerAndZoom(points[0], 10); //临时设置中心点和默认缩放级别 ``` 2. **计算坐标点集合的最佳视野** 使用`BMap.Bounds`类计算所有坐标点的覆盖范围,通过`map.setViewport()`自动调整视图等级: ```javascript var bounds = new BMap.Bounds(); points.forEach(function(point) { bounds.extend(point); //将每个点扩展到bounds范围内 }); map.setViewport(bounds); //自动调整地图中心和缩放级别 ``` 3. **添加导航控件(可选)** 若需用户手动调整视图,可添加鱼骨导航控件: ```javascript map.addControl(new BMap.NavigationControl()); ``` ### 完整示例代码 ```javascript var map = new BMap.Map("container"); var points = [ new BMap.Point(116.404, 39.915), new BMap.Point(117.500, 40.200), new BMap.Point(118.780, 32.070) ]; // 初始化临时中心点 map.centerAndZoom(points[0], 10); // 计算最佳视野 var bounds = new BMap.Bounds(); points.forEach(function(point) { bounds.extend(point); }); map.setViewport(bounds); //关键API调用[^1] // 添加导航控件 map.addControl(new BMap.NavigationControl()); ``` ### 技术原理 - `bounds.extend()`方法会将每个坐标点扩展至矩形覆盖范围 - `setViewport()`会自动计算包含所有点的最小视野,调整地图中心点和缩放级别至最佳状态[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值