简体   繁体   English

调整 Google 地图标记图标图像的大小

[英]Resize Google Maps marker icon image

When I load an image into the icon property of a marker it displays with its original size, which is a lot bigger than it should be.当我将图像加载到标记的 icon 属性中时,它会以其原始大小显示,这比它应该的大小要大得多。

I want to resize to the standard to a smaller size.我想将标准尺寸调整为较小的尺寸。 What is the best way to do this?做这个的最好方式是什么?

Code:代码:

function addMyPos(latitude,longitude){
  position = new google.maps.LatLng(latitude,longitude)
  marker = new google.maps.Marker({
    position: position,
    map: map,
    icon: "../res/sit_marron.png"
  });
}

If the original size is 100 x 100 and you want to scale it to 50 x 50, use scaledSize instead of Size.如果原始尺寸为 100 x 100 并且您想将其缩放到 50 x 50,请使用 scaledSize 而不是 Size。

var icon = {
    url: "../res/sit_marron.png", // url
    scaledSize: new google.maps.Size(50, 50), // scaled size
    origin: new google.maps.Point(0,0), // origin
    anchor: new google.maps.Point(0, 0) // anchor
};

var marker = new google.maps.Marker({
    position: new google.maps.LatLng(lat, lng),
    map: map,
    icon: icon
});

As mentionned in comments, this is the updated solution in favor of Icon object with documentation.正如评论中提到的,这是支持带有文档的 Icon 对象的更新解决方案。

Use Icon object使用图标对象

var icon = {
    url: "../res/sit_marron.png", // url
    scaledSize: new google.maps.Size(50, 50), // scaled size
    origin: new google.maps.Point(0,0), // origin
    anchor: new google.maps.Point(0, 0) // anchor
};

 posicion = new google.maps.LatLng(latitud,longitud)
 marker = new google.maps.Marker({
  position: posicion,
  map: map,
  icon: icon
 });

MarkerImage has been deprecated for Icon 已为 Icon 弃用 MarkerImage

Until version 3.10 of the Google Maps JavaScript API, complex icons were defined as MarkerImage objects.在 Google Maps JavaScript API 3.10 版之前,复杂的图标被定义为 MarkerImage 对象。 The Icon object literal was added in 3.10, and replaces MarkerImage from version 3.11 onwards. Icon 对象文字是在 3.10 中添加的,并从 3.11 版本开始替换 MarkerImage。 Icon object literals support the same parameters as MarkerImage, allowing you to easily convert a MarkerImage to an Icon by removing the constructor, wrapping the previous parameters in {}'s, and adding the names of each parameter. Icon 对象字面量支持与 MarkerImage 相同的参数,允许您通过移除构造函数、将前面的参数包装在 {} 中并添加每个参数的名称来轻松地将 MarkerImage 转换为 Icon。

Phillippe's code would now be:菲利普的代码现在是:

 var icon = {
     url: "../res/sit_marron.png", // url
     scaledSize: new google.maps.Size(width, height), // size
     origin: new google.maps.Point(0,0), // origin
     anchor: new google.maps.Point(anchor_left, anchor_top) // anchor 
 };

 position = new google.maps.LatLng(latitud,longitud)
 marker = new google.maps.Marker({
  position: position,
  map: map,
  icon: icon
 });

Delete origin and anchor will be more regular picture删除原点和锚点会更规则图片

  var icon = {
        url: "image path", // url
        scaledSize: new google.maps.Size(50, 50), // size
    };

 marker = new google.maps.Marker({
  position: new google.maps.LatLng(lat, long),
  map: map,
  icon: icon
 });

If you are using vue2-google-maps like me, the code to set the size looks like this:如果您像我一样使用 vue2-google-maps,则设置大小的代码如下所示:

<gmap-marker
  ..
  :icon="{
    ..
    anchor: { x: iconSize, y: iconSize },
    scaledSize: { height: iconSize, width: iconSize },
  }"
>

A complete beginner like myself to the topic may find it harder to implement one of these answers than, if within your control, to resize the image yourself with an online editor or a photo editor like Photoshop.像我这样的完全初学者可能会发现实现其中一个答案比在您的控制范围内使用在线编辑器或像 Photoshop 这样的照片编辑器自己调整图像大小更难。

A 500x500 image will appear larger on the map than a 50x50 image. 500x500 的图像在地图上会比 50x50 的图像大。

No programming required.无需编程。

So I just had this same issue, but a little different.所以我只是遇到了同样的问题,但有点不同。 I already had the icon as an object as Philippe Boissonneault suggests, but I was using an SVG image.正如Philippe Boissonneault 所建议的那样,我已经将图标作为对象,但我使用的是 SVG 图像。

What solved it for me was:为我解决的是:
Switch from an SVG image to a PNG and following Catherine Nyo on having an image that is double the size of what you will use.从 SVG 图像切换到 PNG 并跟随Catherine Nyo获得一个大小为您将使用的图像大小的两倍的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM