根据地址让高德地图显示多个点

需求:首次进入页面的时候,所有医院的地点标注并显示在地图上;
在这里插入图片描述
当搜索城市的时候,该城市所有医院的地址标注显示在地图上;
在这里插入图片描述
当搜索区、县的时候,该区、县所有医院的地址标注显示在地图上;
在这里插入图片描述
当点击医院的名字的时候,给该医院的地址标注。
在这里插入图片描述
解决:
1:高德地图申请 key (这个就不说了,百度一下)
2:高德地图文档,根据需求我需要的是 :自适应显示多个点标记
在这里插入图片描述
修改html和jq代码:把自适应去掉
3:放到自己项目中 html代码如下
在这里插入图片描述
在这里插入图片描述
4:解决首次进入加载代码 js代码如下

<script>
    $(document).ready(function(){
        function init() {
            $.ajax({
                url:"{:url('index/allhospital')}",
                type:"POST",
                dataType: 'text',
                dataType:"json",
                success: function(data){
                    if(data!=undefined&&data!=null&&data!=""){
                        var map = new AMap.Map('container', {
                            resizeEnable: true,
                            center: [data[0][0], data[0][1]],
                            zoom: 13
                        });
                        map.clearMap();  // 清除地图覆盖物

                        var markers = [];
                        for(var i=0;i<data.length;i++){
                            markers.push({
                                icon:'http://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
                                position:data[i]
                            })
                        }
                        // 添加一些分布不均的点到地图上,地图上添加三个点标记,作为参照
                        markers.forEach(function(marker) {
                            new AMap.Marker({
                                map: map,
                                icon: marker.icon,
                                position: [marker.position[0], marker.position[1]],
                                offset: new AMap.Pixel(-13, -30)
                            });
                        });
                        var newCenter = map.setFitView();
                        document.getElementById('centerCoord').innerHTML = '当前中心点坐标:' + newCenter.getCenter();
                        document.getElementById('tips').innerHTML = '通过setFitView,地图自适应显示到合适的范围内,点标记已全部显示在视野中!';
                    }
                }
            });
        }
        init();    // 调用
    });
</script>

其中主要用到的是如下代码:表示页面刷新就会自动执行:

<script>
    $(document).ready(function(){
        function init() {
        }
        init();    // 调用
    });
</script>

5:城市改动地图改动、区县改动城市改动、点击城市名地图改动。

<script>
    //城市改动
    $("#city").change(function(){
        city = $(this).children('option:selected').val();
        $("#county option").remove();
        $.ajax({
            url:"{:url('index/index')}",
            type:"POST",
            dataType: 'text',
            data:{
                city:city
            },
            dataType:"json",
            success: function(data){
                if(data!=undefined&&data!=null&&data!=""){
                    //添加下拉框县
                    countys = data.countys;
                    if(countys!=undefined&&countys!=null&&countys!=""){
                        var html="";
                        html+="<option value=''>请选择县</option>";
                        for(var i=0;i<countys.length;i++){
                            html+="<option value="+countys[i]["city_id"]+">"+countys[i]["city_name"]+"</option>";
                        }
                        $("#county").html(html);
                    }
                    //添加医院
                    hospitals = data.hospitals;
                    if(hospitals!=undefined&&hospitals!=null&&hospitals!=""){
                        $("#cont1 option").remove();
                        $("#cont2 option").remove();
                        $("#cont3 option").remove();
                        var html1="";
                        var html2="";
                        var html3="";
                        for(var i=0;i<hospitals.length;i++){
                            if(hospitals[i]['hospital_type']=='ContRAst1'){
                                html1+="<option value=\"" + hospitals[i]["id"] + "\">"+hospitals[i]["hospital_name"]+
                                    hospitals[i]["hospital_department"]+hospitals[i]["hospital_tel"]+"</option>";
                            }
                            if(hospitals[i]['hospital_type']=='ContRAst2'){
                                html2+="<option value=\"" + hospitals[i]["id"] + "\">"+hospitals[i]["hospital_name"]+
                                    hospitals[i]["hospital_department"]+hospitals[i]["hospital_tel"]+"</option>";
                            }
                            if(hospitals[i]['hospital_type']=='ContRAst3'){
                                html3+="<option value=\"" + hospitals[i]["id"] + "\">"+hospitals[i]["hospital_name"]+
                                    hospitals[i]["hospital_department"]+hospitals[i]["hospital_tel"]+"</option>";
                            }
                        }
                        $("#cont1").html(html1);
                        $("#cont2").html(html2);
                        $("#cont3").html(html3);
                    }
                    //地图改点
                    positions = data.positions;
                    if(positions!=undefined&&positions!=null&&positions!=""){
                        var map = new AMap.Map('container', {
                            resizeEnable: true,
                            center: [positions[0][0], positions[0][1]],
                            zoom: 13
                        });
                        map.clearMap();  // 清除地图覆盖物
                        var markers = [];
                        for(var i=0;i<positions.length;i++){
                            markers.push({
                                    icon:'http://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
                                    position:positions[i]
                            })
                        }
                        // 添加一些分布不均的点到地图上,地图上添加三个点标记,作为参照
                        markers.forEach(function(marker) {
                            new AMap.Marker({
                                map: map,
                                icon: marker.icon,
                                position: [marker.position[0], marker.position[1]],
                                offset: new AMap.Pixel(-13, -30)
                            });
                        });
                        // 添加事件监听, 使地图自适应显示到合适的范围
                        var newCenter = map.setFitView();
                        document.getElementById('centerCoord').innerHTML = '当前中心点坐标:' + newCenter.getCenter();
                        document.getElementById('tips').innerHTML = '通过setFitView,地图自适应显示到合适的范围内,点标记已全部显示在视野中!';

                    }
                }else{
                    alert.msg(data.message);
                }
            }
        });
    })
    //区、县改动
    $("#county").change(function(){
        county = $(this).children('option:selected').val();
        $.ajax({
            url:"{:url('index/index')}",
            type:"POST",
            dataType: 'text',
            data:{
                county:county
            },
            dataType:"json",
            success: function(data){
                if(data!=undefined&&data!=null&&data!=""){
                    //添加医院
                    hospitals = data.hospitals;
                    if(hospitals!=undefined&&hospitals!=null&&hospitals!=""){
                        $("#cont1 option").remove();
                        $("#cont2 option").remove();
                        $("#cont3 option").remove();
                        var html1="";
                        var html2="";
                        var html3="";
                        for(var i=0;i<hospitals.length;i++){
                            if(hospitals[i]['hospital_type']=='ContRAst1'){
                                html1+="<option value=\"" + hospitals[i]["id"] + "\">"+hospitals[i]["hospital_name"]+hospitals[i]["hospital_department"]+hospitals[i]["hospital_tel"]+"</option>";
                            }
                            if(hospitals[i]['hospital_type']=='ContRAst2'){
                                html2+="<option value=\"" + hospitals[i]["id"] + "\">"+hospitals[i]["hospital_name"]+hospitals[i]["hospital_department"]+hospitals[i]["hospital_tel"]+"</option>";
                            }
                            if(hospitals[i]['hospital_type']=='ContRAst3'){
                                html3+="<option value=\"" + hospitals[i]["id"] + "\">"+hospitals[i]["hospital_name"]+hospitals[i]["hospital_department"]+hospitals[i]["hospital_tel"]+"</option>";
                            }
                        }
                        $("#cont1").html(html1);
                        $("#cont2").html(html2);
                        $("#cont3").html(html3);
                    }
                    //地图改点
                    positions = data.positions;
                    if(positions!=undefined&&positions!=null&&positions!=""){
                        var map = new AMap.Map('container', {
                            resizeEnable: true,
                            center: [positions[0][0], positions[0][1]],
                            zoom: 13
                        });
                        map.clearMap();  // 清除地图覆盖物

                        var markers = [];
                        for(var i=0;i<positions.length;i++){
                            markers.push({
                                icon:'http://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
                                position:positions[i]
                            })
                        }
                        // 添加一些分布不均的点到地图上,地图上添加三个点标记,作为参照
                        markers.forEach(function(marker) {
                            new AMap.Marker({
                                map: map,
                                icon: marker.icon,
                                position: [marker.position[0], marker.position[1]],
                                offset: new AMap.Pixel(-13, -30)
                            });
                        });
                        // 添加事件监听, 使地图自适应显示到合适的范围
                        var newCenter = map.setFitView();
                        document.getElementById('centerCoord').innerHTML = '当前中心点坐标:' + newCenter.getCenter();
                        document.getElementById('tips').innerHTML = '通过setFitView,地图自适应显示到合适的范围内,点标记已全部显示在视野中!';
                    }
                }else{
                    alert.msg(data.message);
                }
            }
        });
    })
    //点击医院出现地址
    $("#cont1").change(function(){
        var city_id = $(this).children('option:selected').val();
        $.ajax({
            url:"{:url('index/findhospttal')}",
            type:"POST",
            dataType: 'text',
            data:{
                city_id:city_id
            },
            dataType:"json",
            success: function(data){
                if(data!=undefined&&data!=null&&data!=""){
                    //地图改点
                    if(data!=undefined&&data!=null&&data!=""){
                        var map = new AMap.Map('container', {
                            resizeEnable: true,
                            center: [data[0], data[1]],
                            zoom: 13
                        });
                        map.clearMap();  // 清除地图覆盖物
                        markers = [{
                            icon: 'http://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
                            position: data
                        }]
                        // 添加一些分布不均的点到地图上,地图上添加三个点标记,作为参照
                        markers.forEach(function(marker) {
                            new AMap.Marker({
                                map: map,
                                icon: marker.icon,
                                position: [marker.position[0], marker.position[1]],
                                offset: new AMap.Pixel(-13, -30)
                            });
                        });
                        // 添加事件监听, 使地图自适应显示到合适的范围
                        var newCenter = map.setFitView();
                        document.getElementById('centerCoord').innerHTML = '当前中心点坐标:' + newCenter.getCenter();
                        document.getElementById('tips').innerHTML = '通过setFitView,地图自适应显示到合适的范围内,点标记已全部显示在视野中!';
                    }
                }else{
                    alert.msg(data.message);
                }
            }
        });
    })
     //点击医院出现地址
    $("#cont2").change(function(){
        var city_id = $(this).children('option:selected').val();
        $.ajax({
            url:"{:url('index/findhospttal')}",
            type:"POST",
            dataType: 'text',
            data:{
                city_id:city_id
            },
            dataType:"json",
            success: function(data){
                if(data!=undefined&&data!=null&&data!=""){
                    //地图改点
                    if(data!=undefined&&data!=null&&data!=""){
                        var map = new AMap.Map('container', {
                            resizeEnable: true,
                            center: [data[0], data[1]],
                            zoom: 13
                        });
                        map.clearMap();  // 清除地图覆盖物

                        markers = [{
                            icon: 'http://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
                            position: data
                        }]
                        // 添加一些分布不均的点到地图上,地图上添加三个点标记,作为参照
                        markers.forEach(function(marker) {
                            new AMap.Marker({
                                map: map,
                                icon: marker.icon,
                                position: [marker.position[0], marker.position[1]],
                                offset: new AMap.Pixel(-13, -30)
                            });
                        });
                        var newCenter = map.setFitView();
                        document.getElementById('centerCoord').innerHTML = '当前中心点坐标:' + newCenter.getCenter();
                        document.getElementById('tips').innerHTML = '通过setFitView,地图自适应显示到合适的范围内,点标记已全部显示在视野中!';
                    }
                }else{
                    alert.msg(data.message);
                }
            }
        });
    })
     //点击医院出现地址
    $("#cont3").change(function(){
        var city_id = $(this).children('option:selected').val();
        console.log(city_id)
        $.ajax({
            url:"{:url('index/findhospttal')}",
            type:"POST",
            dataType: 'text',
            data:{
                city_id:city_id
            },
            dataType:"json",
            success: function(data){
                console.log(data)
                if(data!=undefined&&data!=null&&data!=""){
                    //地图改点
                    if(data!=undefined&&data!=null&&data!=""){
                        var map = new AMap.Map('container', {
                            resizeEnable: true,
                            center: [data[0], data[1]],
                            zoom: 13
                        });
                        map.clearMap();  // 清除地图覆盖物

                        markers = [{
                            icon: 'http://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
                            position: data
                        }]

                        console.log(markers)
                        // 添加一些分布不均的点到地图上,地图上添加三个点标记,作为参照
                        markers.forEach(function(marker) {
                            new AMap.Marker({
                                map: map,
                                icon: marker.icon,
                                position: [marker.position[0], marker.position[1]],
                                offset: new AMap.Pixel(-13, -30)
                            });
                        });
                        // 添加事件监听, 使地图自适应显示到合适的范围
                        var newCenter = map.setFitView();
                        document.getElementById('centerCoord').innerHTML = '当前中心点坐标:' + newCenter.getCenter();
                        document.getElementById('tips').innerHTML = '通过setFitView,地图自适应显示到合适的范围内,点标记已全部显示在视野中!';
                    }
                }else{
                    alert.msg(data.message);
                }
            }
        });
    })
</script>

6:后台需要用到的是根据医院详细地址计算出医院经纬度

    /**
     * 根据地址计算经纬度
     * @param $address
     * @return array|bool
     */
    function addresstolatlag($address)
    {
        $address = trim($address);
        $url = 'http://restapi.amap.com/v3/geocode/geo?address=' . $address . '&key=我的高德地图的 key ';
        if ($result = file_get_contents($url)) {
            $result = json_decode($result, true);
            //判断是否成功
            if (!empty($result['count'])) {
                return explode(',', $result['geocodes']['0']['location']);
            } else {
                return false;
            }
        }
    }
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值