Change Google Map marker color with external jQuery

mixerowsky

I'm using this function to draw the marker:

function pinSymbol(color) {
    return {
        path: 'M31.5,0C14.1,0,0,14,0,31.2C0,53.1,31.5,80,31.5,80S63,52.3,63,31.2C63,14,48.9,0,31.5,0z M31.5,52.3 c-11.8,0-21.4-9.5-21.4-21.2c0-11.7,9.6-21.2,21.4-21.2s21.4,9.5,21.4,21.2C52.9,42.8,43.3,52.3,31.5,52.3z',
        fillColor: color,
        fillOpacity: 1,
        strokeColor: '#000',
        strokeWeight: 0,
        scale: 1,
   };
}

and this is my marker:

var marker = new google.maps.Marker({
position: map.getCenter(),
icon: pinSymbol("#fff"), //defined marker color
labelText: 'HERE WE ARE',
labelVisible: true,
labelClass: "label",
labelZIndex: 99,
draggable: false,
map: map
});

I have a color pallete in other file with the jQuery script that changes a lot of colors on the page when user choose some color, but I don't know how to change color of this marker also.

Can I somehow change color of the marker externaly, in some other file?

Atilla Ozgur

When you create a variable using var keyword in javascript that is a local variable. If you want to reach that variable from another javascript file, you need to pass this reference around or use a global variable. With global variable approach, easiest to understand but in the long run hardest to maintain, you may do something like following:

CreateMap.js

<script type="text/javascript">

function pinSymbol(color) {
    return {
        path: 'M31.5,0C14.1,0,0,14,0,31.2C0,53.1,31.5,80,31.5,80S63,52.3,63,31.2C63,14,48.9,0,31.5,0z M31.5,52.3 c-11.8,0-21.4-9.5-21.4-21.2c0-11.7,9.6-21.2,21.4-21.2s21.4,9.5,21.4,21.2C52.9,42.8,43.3,52.3,31.5,52.3z',
        fillColor: color,
        fillOpacity: 1,
        strokeColor: '#000',
        strokeWeight: 0,
        scale: 1,
   };
}
  function initialize() {
    var mapOptions = {
      center: { lat: -34.397, lng: 150.644},
      zoom: 8
    };
    var map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);

    var marker = new google.maps.Marker({
        position: map.getCenter(),
        icon: pinSymbol("#fff"), //defined marker color
        labelText: 'HERE WE ARE',
        labelVisible: true,
        labelClass: "label",
        labelZIndex: 99,
        draggable: false,
        map: map
    });

    // global variables set
    window.currentMap = map;
    window.mainMarker = marker;


  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>

AnotherMapScript.js

<script type="text/javascript">

    var marker = window.mainMarker;
    marker.setIcon(pinSymbol('#f20'));

</script>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Android Google Map Marker With Label?

분류에서Dev

Google map marker not displaying dynamically via ajax

분류에서Dev

google map marker category filtering is not working?

분류에서Dev

How to set the marker color to black in google maps android

분류에서Dev

change background color on webpage jQuery

분류에서Dev

Set Image from drawable as marker in Google Map version 2

분류에서Dev

Android google map v2 error at marker's icon

분류에서Dev

How to display info box over google map marker

분류에서Dev

문제 -Jquery with Google Map

분류에서Dev

Change only color of * in div using jquery

분류에서Dev

Using jQuery Switch Case to Change text Color

분류에서Dev

How can i change hyperlink color with JQuery?

분류에서Dev

Add marker to map on tap

분류에서Dev

DB에서 Google Map Marker가 작동하지 않음

분류에서Dev

How can i show the area in a different color in Google Map

분류에서Dev

Google Map InfoBox의 jQuery UI 탭

분류에서Dev

Why Does Map Marker Lurch Around The Map

분류에서Dev

Google Maps jQuery Marker가 표시되지 않음

분류에서Dev

Google Column Chart change color for each column with json

분류에서Dev

Google Column Chart change color for each column with json

분류에서Dev

How to change section color of a stacked bar chart in Google Charts API?

분류에서Dev

Change background-position horizontal transition for color on scroll jQuery/CSS

분류에서Dev

Jquery Change color by input[type=text] keydown not working

분류에서Dev

jQuery checkbox select all and change background color on select

분류에서Dev

jQuery TimeAgo library to change color of Text if the DateTime is in the Past?

분류에서Dev

dom-repeat의 google-map-marker에 바인딩 문제가 있습니다.

분류에서Dev

Making Google maps marker array global breaks marker click event

분류에서Dev

Restrict google maps API marker to only one click per marker

분류에서Dev

Map discrete value to color

Related 관련 기사

  1. 1

    Android Google Map Marker With Label?

  2. 2

    Google map marker not displaying dynamically via ajax

  3. 3

    google map marker category filtering is not working?

  4. 4

    How to set the marker color to black in google maps android

  5. 5

    change background color on webpage jQuery

  6. 6

    Set Image from drawable as marker in Google Map version 2

  7. 7

    Android google map v2 error at marker's icon

  8. 8

    How to display info box over google map marker

  9. 9

    문제 -Jquery with Google Map

  10. 10

    Change only color of * in div using jquery

  11. 11

    Using jQuery Switch Case to Change text Color

  12. 12

    How can i change hyperlink color with JQuery?

  13. 13

    Add marker to map on tap

  14. 14

    DB에서 Google Map Marker가 작동하지 않음

  15. 15

    How can i show the area in a different color in Google Map

  16. 16

    Google Map InfoBox의 jQuery UI 탭

  17. 17

    Why Does Map Marker Lurch Around The Map

  18. 18

    Google Maps jQuery Marker가 표시되지 않음

  19. 19

    Google Column Chart change color for each column with json

  20. 20

    Google Column Chart change color for each column with json

  21. 21

    How to change section color of a stacked bar chart in Google Charts API?

  22. 22

    Change background-position horizontal transition for color on scroll jQuery/CSS

  23. 23

    Jquery Change color by input[type=text] keydown not working

  24. 24

    jQuery checkbox select all and change background color on select

  25. 25

    jQuery TimeAgo library to change color of Text if the DateTime is in the Past?

  26. 26

    dom-repeat의 google-map-marker에 바인딩 문제가 있습니다.

  27. 27

    Making Google maps marker array global breaks marker click event

  28. 28

    Restrict google maps API marker to only one click per marker

  29. 29

    Map discrete value to color

뜨겁다태그

보관