双击时如何创建高亮显示图像?

时间:2015-11-17 09:08:16

标签: javascript jquery css google-maps highlight

当用户双击图像的某个部分时,我正在尝试显示突出显示的圆圈。例如,如果图像是城市的地图,并且他们双击coord:(X,Y),我希望突出显示的圆圈的中心位于(X,Y),并且半径会因项目而异点击了。

这是我到目前为止所做的:

<script type="text/javascript"
      src="https://maps.googleapis.com/maps/api/js?sensor=false">
   </script>         
<script type="text/javascript"> 
 function showMap(){        
        var mapOptions = {
                     zoom: 19,
                     center: new google.maps.LatLng(x, y),//let z, y be some coord
                     mapTypeId: google.maps.MapTypeId.ROADMAP,
                     disableDoubleClickZoom: true,
                     disableDefaultUI: true,
                     zoomControl: false,
                     draggable: false, 
                     scrollwheel: false
                }
        map = new google.maps.Map(document.getElementById("myMap"), 
                        mapOptions);

        google.maps.event.addListener(map, 'dblclick', doubleClicked);
    }

    function doubleClicked(e){//highlight the area with a circle                
                //alert("lat: " + e.latLng.lat() + "\nlong: " + e.latLng.lng());
    }        
 </script>

HTML

<div id="myMap" ></div>

CSS

#myMap {
    margin-left: auto;
    margin-right: auto;
    width: 1200px;
    height: 800px
}

在@Aamir Sarwar的帮助下解决方案:

var circle = new google.maps.Circle({
                strokeColor: '#FFFFFF',
                strokeOpacity: 0.8,
                strokeWeight: 2,
                fillColor: '#FF0000',
                fillOpacity: 0.35,
                map: map,
                center: {lat: X, lng: Y}
                radius: 10
            });

2 个答案:

答案 0 :(得分:3)

检查以下示例副本来自此链接...希望它可以帮助您https://developers.google.com/maps/documentation/javascript/examples/rectangle-simple?hl=en

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Rectangles</title>
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
    <script>

// This example adds a red rectangle to a map.

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 11,
    center: {lat: 33.678, lng: -116.243},
    mapTypeId: google.maps.MapTypeId.TERRAIN
  });

  var rectangle = new google.maps.Rectangle({
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35,
    map: map,
    bounds: {
      north: 33.685,
      south: 33.671,
      east: -116.234,
      west: -116.251
    }
  });
}

    </script>
  </head>
  <body>
    <div id="map"></div>
    <script async defer
        src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&signed_in=true&callback=initMap"></script>
  </body>
</html>

答案 1 :(得分:3)

以下是概要:

  • myMap应该是position:relative

  • 在myMap上添加鼠标事件以跟踪鼠标位置的X,Y

  • 在doubleClicked方法中,创建将显示为圆的div。它也应该是position:absolute

  • 将该元素添加到myMap并更改其CSS:左侧为鼠标X位置,顶部为鼠标Y位置