123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- @{
- ViewBag.Title = "百度地图";
- Layout = "~/Views/Shared/_BaiduMap.cshtml";
- }
- <div class="main-div">
- <div id='allmap' style='width: 100%; height: 100%; position: absolute;'></div>
- <table cellspacing="1" width="100%" style="position: absolute;top: 20px;">
- <tr>
- <td>
- <span style="margin-left: 30px">关键字:</span>
- <input type="text" id="address" value="" />
- <input type="button" value="搜索" onclick="searchAddress()" />
- </td>
- <td></td>
- <td>经度</td>
- <td>
- <input type="text" name="lng" id="lng" value="" />
- </td>
- <td>纬度</td>
- <td>
- <input type="text" name="lat" id="lat" value="" />
- </td>
- <td>地址</td>
- <td>
- <input type='text' value='' name='sever_add' id='sever_add' readonly />
- </td>
- </tr>
- </table>
- </div>
- <script>
- function validate() {
- var sever_add = document.getElementsByName('sever_add')[0].value;
- if (isNull(sever_add)) {
- alert('请选择地址');
- return false;
- }
- return true;
- }
- //判断是否是空
- function isNull(a) {
- return (a == '' || typeof (a) == 'undefined' || a == null) ? true : false;
- }
- var map = new BMap.Map("allmap");
- var geoc = new BMap.Geocoder(); //地址解析对象
- var markersArray = [];
- var geolocation = new BMap.Geolocation();
- var point = new BMap.Point("@System.Configuration.ConfigurationManager.AppSettings["LngAndLat"].Split('|')[0]", "@System.Configuration.ConfigurationManager.AppSettings["LngAndLat"].Split('|')[1]");
- map.centerAndZoom(point, 12); // 中心点
- map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
- //geolocation.getCurrentPosition(function (r) {
- // if (this.getStatus() == BMAP_STATUS_SUCCESS) {
- // var mk = new BMap.Marker(r.point);
- // map.addOverlay(mk);
- // map.panTo(r.point);
- // map.enableScrollWheelZoom(true);
- // }
- // else {
- // alert('failed' + this.getStatus());
- // }
- //}, { enableHighAccuracy: true })
- map.addEventListener("click", showInfo);
- var local = new BMap.LocalSearch(map, {
- renderOptions: { map: map }
- });
- //清除标识
- function clearOverlays() {
- if (markersArray) {
- for (i in markersArray) {
- map.removeOverlay(markersArray[i])
- }
- }
- }
- //地图上标注
- function addMarker(point) {
- var marker = new BMap.Marker(point);
- markersArray.push(marker);
- clearOverlays();
- map.addOverlay(marker);
- }
- //点击地图时间处理
- function showInfo(e) {
- document.getElementById('lng').value = e.point.lng;
- document.getElementById('lat').value = e.point.lat;
- geoc.getLocation(e.point, function (rs) {
- var addComp = rs.addressComponents;
- var address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
- document.getElementById('sever_add').value = address;
- });
- addMarker(e.point);
- }
- // 搜索地址
- function searchAddress() {
- var value = document.getElementById('address').value;
- if (!(value === "")) {
- local.search(value);
- }
- }
- // 返回数据
- function AcceptClick(callBack) {
- var lng = document.getElementById('lng').value;
- var lat = document.getElementById('lat').value;
- if (!(lng === "" || lat === "")) {
- callBack(document.getElementById('lng').value + "|" + document.getElementById('lat').value);
- dialogClose();
- }
- }
- </script>
|