簡體   English   中英

Google Maps Api,自定義標記錨點

[英]Google Maps Api, Custom marker anchor points

我創建了一個自定義標記用於我的谷歌地圖。 圖標的錨點應該在左下角。 在我的地圖上,錨點似乎位於圖標中心的底部邊緣。 圖標是 32 x 49。我包含的代碼放置了我所有的標記。 我已經搜索了幾個小時,找不到任何答案。 V3 api 說該屬性是錨點。 當我使用該屬性時,地圖上不會顯示任何標記。

var iconImage = "images/Italian Flag Mine New.png";

var map = new google.maps.Map(document.getElementById('map-canvas'), {
    zoom: 12,
    center: new google.maps.LatLng(45.067314, 7.697774),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var iconImage = {
    url: "images/Italian Flag Mine New.png",
    anchorPosition: (0, 49)
};

//var iconImage = {url: "images/Italian Flag Mine New.png",
//               anchor :  (0,49)};

var marker, i, savedMarker;

for (i = 0; i < locations.length; i++) {
    marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][latIndex], locations[i][lngIndex]),
        map: map,
        animation: google.maps.Animation.DROP,
        icon: iconImage
    });
}

anchor屬性是標記構造函數的一部分,但您仍然必須根據Google 的文檔(在“復雜圖標”標題下)創建對象的實例。 試試這個:

var iconImage = {
  url: "images/Italian Flag Mine New.png",
  anchor: new google.maps.Point(0,49)
};

您還應該考慮指定 size 屬性。

您需要指定可點擊錨點的坐標,這取決於您將要進行的縮放:

https://developers.google.com/maps/documentation/javascript/markers

// Shapes define the clickable region of the icon.
  // The type defines an HTML &lt;area&gt; element 'poly' which
  // traces out a polygon as a series of X,Y points. The final
  // coordinate closes the poly by connecting to the first
  // coordinate.
    var shape = {
        coord: [9, 0, 6, 1, 4, 2, 2, 4, 0, 8, 0, 12, 1, 14, 2, 16, 5, 19, 7, 23, 8, 26, 9, 30, 9, 34, 11, 34, 11, 15, 49, 16, 49, 30, 12, 26, 13, 24, 14, 21, 16, 18, 18, 16, 20, 12, 20, 8, 18, 4, 16, 2, 15, 1, 13, 0],
        type: 'poly'
    };
    var iconImage=  new google.maps.MarkerImage(
                             // URL
                             "images/Italian Flag Mine New.png",//желательно название картинки поменять, чтобы было без пробелов
                             // (width,height)
                             new google.maps.Size(32, 49),
                             // The origin point (x,y)
                             new google.maps.Point(0, 0),
                             // The anchor point (x,y)
                             new google.maps.Point(9, 49)
                            );
 marker = new google.maps.Marker({
      position : new google.maps.LatLng(locations[i][latIndex], locations[i][lngIndex]),
      map      : map,
      animation: google.maps.Animation.DROP,
      icon     : iconImage,
      shape: shape
     });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM