添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

How to add label to Google map marker? (Error: InvalidValueError: setLabel: not a string; and no text property)

Ask Question

I want to add label to google map marker based on the number of my GPS locations. I got my data in my database I am able to add markers to my map but what I am unable to do is add markers inside the marker.

for(var i = 0; i < data.length; i++) {
   var coords = data[i].GPSCoordinates.split(',');
   var position = new google.maps.LatLng(coords[0], coords[1]);
   var labels = i + 1;
   addMarker(position, map, labels);
function addMarker(location, map, label) {
   var marker = new google.maps.Marker({
       position: location,
       map: map,
       label: label

I get a javascript error with your code: InvalidValueError: setLabel: not a string; and no text property. The value assigned to the label property must be a string (or a MarkerLabel anonymous object). The code is currently assigning a number. Change:

var labels = i + 1;
var labels = ""+ (i + 1);

proof of concept fiddle

code snippet:

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1660756),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
  var data = [{GPSCoordinates: "37.4419, -122.1419"},
    {GPSCoordinates: "37.4529598, -122.1817252"},
    {GPSCoordinates: "37.4335499, -122.2030209"},
    {GPSCoordinates: "37.424106, -122.1660756"}
  for (var i = 0; i < data.length; i++) {
    var coords = data[i].GPSCoordinates.split(',');
    var position = new google.maps.LatLng(coords[0], coords[1]);
    var labels = "" + (i + 1);
    addMarker(position, map, labels);
  function addMarker(location, map, label) {
    var marker = new google.maps.Marker({
      position: location,
      map: map,
      label: label
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

The value assigned to the label property in google map must be a string. in your code, it is currently assigning a number. please use something like this:

var labels = (i+1).toString()
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.