leafletjs自定义标记位置不对

时间:2014-03-28 07:02:09

标签: javascript leaflet

当我为传单js添加自定义标记图标时,标记图标未正确定位。

当我使用自定义标记http://jsfiddle.net/amrana83/7k5Jr/

时,这是一个小提琴

以下是我使用自定义标记时的代码

<style>
  html, body, #map {
    height: 500px;
    width: 800px;
    margin: 0px;
    padding: 0px
  }
  .leaflet-map-pane {
    z-index: 2 !important;
  }

  .leaflet-google-layer {
    z-index: 1 !important;
  }
</style>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://matchingnotes.com/javascripts/leaflet-google.js"></script>
  <body>
    <div id="map"></div>
    <script>
      var map = new L.Map('map', {center: new L.LatLng(51.5, -0.09), zoom: 4});
      var googleLayer = new L.Google('ROADMAP');
      map.addLayer(googleLayer);
      var greenIcon = new L.Icon({iconUrl: 'http://technobd.rvillage.com/application/modules/Rvillage/externals/images/all_members.png'});
      L.marker([51.5, -0.09], {icon: greenIcon}).bindPopup("I am a green leaf.").addTo(map);//using custom marker
      L.marker([60.5, -0.09], {}).bindPopup("I am a green leaf.").addTo(map);
    </script>
  </body>

当我不使用自定义标记http://jsfiddle.net/amrana83/8skPU/1/

时,这是一个小提琴

以下是我未使用自定义标记

时的代码
<style>
  html, body, #map {
    height: 500px;
    width: 800px;
    margin: 0px;
    padding: 0px
  }
  .leaflet-map-pane {
    z-index: 2 !important;
  }

  .leaflet-google-layer {
    z-index: 1 !important;
  }
</style>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://matchingnotes.com/javascripts/leaflet-google.js"></script>
  <body>
    <div id="map"></div>
    <script>
      var map = new L.Map('map', {center: new L.LatLng(51.5, -0.09), zoom: 4});
      var googleLayer = new L.Google('ROADMAP');
      map.addLayer(googleLayer);
      L.marker([51.5, -0.09], {}).bindPopup("I am a green leaf.").addTo(map);//not using custom marker
      L.marker([60.5, -0.09], {}).bindPopup("I am a green leaf.").addTo(map);
    </script>
  </body>

3 个答案:

答案 0 :(得分:21)

您必须指定图标的大小,如下所示:

var greenIcon = new L.Icon({
    iconUrl: 'http://technobd.rvillage.com/application/modules/Rvillage/externals/images/all_members.png',
    iconSize: [41, 51], // size of the icon
    iconAnchor: [20, 51], // point of the icon which will correspond to marker's location
    popupAnchor: [0, -51] // point from which the popup should open relative to the iconAnchor                                 
});

答案 1 :(得分:1)

当我看到http://leafletjs.com/reference.html#icon时,我看到使用自定义标记图标我必须更改图标的位置http://leafletjs.com/reference.html#icon-iconanchor使图标位置正确,我们可以更改为正确定位。

在解决问题http://jsfiddle.net/amrana83/xv8m9/1/

后,这是一个小提琴
var LeafIcon = L.Icon.extend({
    options: {
        iconAnchor:   [19, 46],//changed marker icon position
        popupAnchor:  [0, -36]//changed popup position
    }
});
  var greenIcon = new LeafIcon({iconUrl: 'http://technobd.rvillage.com/application/modules/Rvillage/externals/images/all_members.png'});

答案 2 :(得分:1)

你可以尝试这种方式,它很容易:)

首先为标记
创建CSS  .cd-single-point {
  位置:绝对;
  list-style-type:none;
  左: left_pos px;
  顶部: top_pos px;
}
然后调用Javascript调整位置,如此

div.style.left =(point.x- (left_pos / 2))+&#39; px&#39 ;;
div.style.top =(point.y- (top_pos / 2))+&#39; px&#39 ;;

......我认为取决于你得到的情况 您可以更改增量位置的值。 这是我的结果 希望这有帮助!