layui+高德获取经纬度(可点击更换位置)

2020-08-07  本文已影响0人  zmindthegap
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加</title>
    <meta name="renderer" content="webkit">
    <meta name="author" content="zhanhaisheng">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="{{asset('static')}}/layuiadmin/layui/css/layui.css" media="all">
</head>
<style>
    #container{
        height:100%;
        width:100%;
        text-align: center;
    }
</style>
<body>
<div class="layui-form" lay-filter="layuiadmin-form-admin" id="layuiadmin-form-admin" style="padding: 20px 30px 0 0;">
    <div class="layui-form-item">
        <label class="layui-form-label">父类</label>
        <div class="layui-input-block">
            <select name="pid" lay-verify="required" lay-search required>
                <option value="0">顶级分类</option>
                @foreach($cate as $value)
                    <option value="{{$value->id}}">{{$value->html.$value->title}}</option>
                @endforeach
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">
            <input name="title" placeholder="请输入" class="layui-input" type="text" required lay-verify="required" autocomplete="off" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否末级</label>
        <div class="layui-input-inline">
            <input type="radio" name="end_status"  value="1" title="是">
            <input type="radio" name="end_status"  value="0" title="否" checked>
        </div>
        <div class="layui-form-mid layui-word-aux">无下级菜单时,请选择 ‘是’.</div>
    </div>
    <div align="center" style="width: 100%;height: 340px;padding-left: 12px;">
        <div id="container"></div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="hidden" name="longitude">
        <input type="hidden" name="latitude">
        <input type="button" lay-submit lay-filter="LAY-submit" id="LAY-submit" value="确认">
    </div>
</div>

<script src="{{asset('static')}}/layuiadmin/layui/layui.js"></script>
<script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.14&key=yourkey&plugin=AMap.Geocoder"></script>
<script>
layui.config({
    base: '{{asset('static')}}/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'form'], function(){
    var $ = layui.$
    ,form = layui.form
    ;

    var map = new AMap.Map("container", {
        resizeEnable: true
    });
    
    var geocoder,marker;
    function geoCode() {
        if(!geocoder){
            geocoder = new AMap.Geocoder({
                  city: "0574" //仅浙江范围
            });
        }
        var address  = $('input[name=title]').val();

        if(address == ''){
            //默认给宁波这个位置
            address = '宁波';
        }
        geocoder.getLocation(address, function(status, result) {
            if (status === 'complete'&&result.geocodes.length) {
                var lnglat = result.geocodes[0].location
                $('input[name=longitude]').val(lnglat.lng);
                $('input[name=latitude]').val(lnglat.lat);
                if(!marker){
                    marker = new AMap.Marker();
                    map.add(marker);
                }
                marker.setPosition(lnglat);
                map.setFitView(marker);
            }else{
                // layer.msg('请输入正确的地址');
            }
        });
    };

    var clickHandler = function(e) {
        $('input[name=longitude]').val(e.lnglat.lng);
        $('input[name=latitude]').val(e.lnglat.lat);
        console.log(e.lnglat);
        marker.setPosition(e.lnglat);
    };

    map.on('click', clickHandler);

    $("input[name=title]").keydown(function(e){
        if (e.keyCode === 13) {
            geoCode();
            return false;
        }
        return true;
    });

    $("input[name=title]").keyup(function(){
        geoCode();
        return false;
    });


})
</script>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读