Resize Google Maps marker icon image

后端 未结 7 1579
感动是毒
感动是毒 2020-11-29 17:41

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.

I want to resize to the standard

相关标签:
7条回答
  • 2020-11-29 18:07

    If the original size is 100 x 100 and you want to scale it to 50 x 50, use scaledSize instead of 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
    });
    
    0 讨论(0)
  • 2020-11-29 18:08

    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.

    A 500x500 image will appear larger on the map than a 50x50 image.

    No programming required.

    0 讨论(0)
  • 2020-11-29 18:19

    As mentionned in comments, this is the updated solution in favor of Icon object with documentation.

    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
     });
    
    0 讨论(0)
  • 2020-11-29 18:22

    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.

    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.

    0 讨论(0)
  • 2020-11-29 18:23

    If you are using vue2-google-maps like me, the code to set the size looks like this:

    <gmap-marker
      ..
      :icon="{
        ..
        anchor: { x: iconSize, y: iconSize },
        scaledSize: { height: iconSize, width: iconSize },
      }"
    >
    
    0 讨论(0)
  • 2020-11-29 18:24

    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
     });
    
    0 讨论(0)
提交回复
热议问题