js中通过点击事件或输入关键词的方式获取地图中的经纬度

H5页面 

<html>
<head>
  <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>地图服务</title>
	<!--引入高德地图JSAPI -->
	<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
    <script src="http://cache.amap.com/lbs/static/es5.min.js"></script>
    <script src="http://webapi.amap.com/maps?v=1.4.0&key=7c1b6b2cfb952c65ee87707ef162beca&plugin=AMap.Autocomplete,AMap.PlaceSearch"></script>
	  <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
	  <style>
        html, body, #container {
            height: 100%;
            width: 100%;
        }

        .amap-icon img,
        .amap-marker-content img{
            width: 25px;
            height: 34px;
        }

        .marker {
            position: absolute;
            top: -20px;
            right: -118px;
            color: #fff;
            padding: 4px 10px;
            box-shadow: 1px 1px 1px rgba(10, 10, 10, .2);
            white-space: nowrap;
            font-size: 12px;
            font-family: "";
            background-color: #25A5F7;
            border-radius: 3px;
        }

        .input-card{
            width: 32rem;
            z-index: 170;
        }

        .input-card .btn{
            margin-right: .8rem;
        }

        .input-card .btn:last-child{
            margin-right: 0;
        }
    </style>
</head>
<body>

<div id="container"></div>
<div id="myPageTop">
    <table>
        <tr>
            <td>
                <label>请输入关键字:</label>
            </td>
        </tr>
        <tr>
            <td>
                <input id="tipinput" />
            </td>
        </tr>
    </table>
</div>
</body>
</html>

js代码实现

<script>
var map,marker;
var geocoder;
var placeSearch;
$(function(){
    // 加入高的地图
   map = new AMap.Map('container', {
        resizeEnable: true,
        zoom: 11,
        center: [116.40, 39.91]
    });
	//为地图注册click事件获取鼠标点击出的经纬度坐标
    map.on('click', function(e) {
		alert( e.lnglat.getLng() + ',' + e.lnglat.getLat());
		clearMarker();
		addMarker(e.lnglat.getLng(),e.lnglat.getLat());
    });
	 //输入提示
    var autoOptions = {
        input: "tipinput"
    };
    var auto = new AMap.Autocomplete(autoOptions);
    var placeSearch = new AMap.PlaceSearch({
        map: map
    });  //构造地点查询类
    AMap.event.addListener(auto, "select", select);//注册监听,当选中某条记录时会触发
    function select(e) {
		console.log(e);
        placeSearch.setCity(e.poi.adcode);
        placeSearch.search(e.poi.name);  //关键字查询查询
    }
	
	AMap.event.addListener(placeSearch, "markerClick", function(e){
        console.log(e.data.location);//当前marker的经纬度信息
		alert(e.data.location.lng + ',' + e.data.location.lat);
	});
	
});
    // 实例化点标记
    function addMarker(lng,lat) {
        marker = new AMap.Marker({
            icon: "poi-marker-default.png",
            position: [lng,lat],
            offset: new AMap.Pixel(-13, -30)
        });
        marker.setMap(map);
    }
	
 // 清除 marker
    function clearMarker() {

        if (marker) {
            marker.setMap(null);
            marker = null;
        }
    }
	
	
</script>

实现效果

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值