Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace google map marker icon with custom icon in @angular/google-maps module

component.html:

  <google-map (mapClick)="click($event)" 
  [center]="center" [options]="options" height="500px" width="100%"
    [zoom]="zoom">

    <map-marker (mapClick)="openInfoWindow(marker)" [clickable]="true"
  
    *ngFor="let marker of markers" [position]="marker.position" [label]="marker.label"
      [title]="marker.title" [options]="marker.options">
    </map-marker>
    <map-info-window>Info Window content</map-info-window>
  </google-map>

component.ts:

options: google.maps.MapOptions = {
    zoomControl: true,
    scrollwheel: false,
    disableDoubleClickZoom: true,
    maxZoom: 15,
    minZoom: 8,
  }

I've tried adding icon property with url value in options, passing input in map-marker but none worked

https://github.com/angular/components/tree/master/src/google-maps#readme

like image 463
Viraj Singh Avatar asked Jul 02 '20 07:07

Viraj Singh


People also ask

How do I add a logo to a pin on Google Maps?

Click the name of the pin you've just added on the menu panel on the upper left corner of the web page, and click the paint bucket icon next to it. A small dialog box with a color picker will appear. Click "More Icons" on the box, and a larger dialog box labeled "Choose your icon" will show.

How do I change the marker icon on Google Maps flutter?

First, create a method that handles the asset path and receives a size (this can be either the width, height, or both, but using only one will preserve ratio). Then, just add it to your map using the right descriptor: final Uint8List markerIcon = await getBytesFromAsset('assets/images/flutter.


2 Answers

As for Angular Google Maps Marker (https://github.com/angular/components/tree/master/src/google-maps/map-marker) documentation, options includes all map-marker object properties that are not "convenience inputs":

...this component offers an options input as well as convenience inputs for position, title, label, and clickable, and supports all google.maps.Marker events as outputs.

so any other property that's not a "convenience input" has to be passed inside a JSON object like so to each marker object on your array:

      const markers =[
      {...},
      {...},
      [{
        position:{
           lat: 24.1  + ((Math.random() - 0.5) * 2) / 100,
           lng: 0.2 + ((Math.random() - 0.5) * 2) / 100,
        },
        visible: true,
        opacity: 0.6,
        label: {
           color: '#333',
           text: 'My Label',
        },
        title: 'My Title',
           options = {
           draggable: false, 
           icon: '../assets/icons/MyMarker.png'
        }
      }],
      {...},
      {...}
    ];

or, at least, it's what has to be inferred until documentation is completed.

Just past this code on your component and fill the ellipsis objects with needed data or just create an array with this structure from your own data.

like image 66
JoeCool Avatar answered Dec 28 '22 09:12

JoeCool


A little digging into the node-modules of @angular/google-maps I found out the icons should be set in the options of map marker.

 <map-marker (mapClick)="openInfoWindow(marker)" [clickable]="true"
  
    *ngFor="let marker of markers" [position]="marker.position" [label]="marker.label"
      [title]="marker.title" [options]="marker.options">
    </map-marker>

for example the marker property should look like

     const markers = [{

        position:{
        lat: 27  + ((Math.random() - 0.5) * 2) / 10,
        lng: 80 + ((Math.random() - 0.5) * 2) / 10,
      },
        visible: false,
        opacity: 0.1,
        label: {
          color: 'black',
          text: 'Marker label ',
        },
        title: 'Marker title ',
        options: {
          animation: google.maps.Animation.DROP,
          icon: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'
        }
      }]
like image 26
Viraj Singh Avatar answered Dec 28 '22 11:12

Viraj Singh