我如何向用户询问标记图标类型?

How do i ask user for a marker icon type?

我设法向用户询问 Lat 和 lNG,但我似乎无法向用户询问标记的图标类型。

图标当前设置为

icon: lH

选择是:lN、lH 和 lC,您可以在下方看到一点,我尝试使用提示 var 并将其设置为图标,但没有用

  var icons = prompt("lH, lN, or lC", "lC");
  icon: icons

有人可以帮忙吗?

 function newqMarker( width, height ) {



          lN = new google.maps.MarkerImage( 'http://www.schweizer-brandschutz.ch/media/image/google-marker.png',
      new google.maps.Size( 32, 37 ),
      new google.maps.Point( 0, 0 ), 
      new google.maps.Point( 16, 35 ) );

      lH = new google.maps.MarkerImage( 'http://www.andersiahotel.pl/img/marker-small.png', 
      new google.maps.Size( 32, 37 ), 
      new google.maps.Point( 0, 0 ),
      new google.maps.Point( 16, 35 ) );

      lC = new google.maps.MarkerImage( 'https://oge.com/images/blueMarker.png',
      new google.maps.Size( 20, 32 ),
      new google.maps.Point( 0, 0 ),
      new google.maps.Point( 16, 35  ) );

      var person = prompt("lH, lN, or lC", "lC");

        markere = new google.maps.Marker( {
            position: new google.maps.LatLng( prompt( "Latitude: " ), prompt( "Longtitude: " ) ),
            map: map,
            animation: google.maps.Animation.DROP,
            icon: lH,
            title: 'I am here'
        } );
        google.maps.event.addListener( markere, 'click', toggleBounce );
        google.maps.event.addListener( markere, 'click', function() {
            InfoWindowe.open( map, markere )
        } );



  google.maps.event.addListener(markere, 'mouseover', function(){this.setIcon(lH)});
 google.maps.event.addListener(markere, 'mouseout', function(){this.setIcon(lN)});
 google.maps.event.addListener(markere, 'mousedown', function(){this.setIcon(lC)});
 google.maps.event.addListener(markere, 'mouseup', function(){this.setIcon(lH)});


  }

图标是一个复杂的对象,它不能由prompt返回,但你可以添加一个switch语句来设置属性从返回的字符串中prompt:

var person = prompt("lH, lN, or lC", "lC");
var icon = lH;
switch (person) {
  case "lH":
    icon = lH;
    break;
  case "lN":
    icon = lN;
    break;
  case "lC":
  default:
    icon = lC;
    break;
}

var markere = new google.maps.Marker({
  position: new google.maps.LatLng(prompt("Latitude: "), prompt("Longtitude: ")),
  map: map,
  animation: google.maps.Animation.DROP,
  icon: icon,
  title: 'I am here'
});
google.maps.event.addListener(markere, 'click', toggleBounce);
google.maps.event.addListener(markere, 'click', function() {
  InfoWindowe.open(map, markere)
});

proof of concept fiddle

注意: MarkerImage class 在 v3.10 之后被弃用(很久以前~2012/2013):

来自 the documentation

Converting MarkerImage objects to type Icon

Until version 3.10 of the Google Maps JavaScript API, complex icons were defined as MarkerImage objects. The Icon object literal was added in version 3.10, and replaces MarkerImage from version 3.11 onwards.

代码片段:

var map;

function initialize() {
  map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  newqMarker(0, 0);
}
google.maps.event.addDomListener(window, "load", initialize);

function newqMarker(width, height) {
  lN = new google.maps.MarkerImage('http://www.schweizer-brandschutz.ch/media/image/google-marker.png',
    new google.maps.Size(32, 37),
    new google.maps.Point(0, 0),
    new google.maps.Point(16, 35));

  lH = new google.maps.MarkerImage('http://www.andersiahotel.pl/img/marker-small.png',
    new google.maps.Size(32, 37),
    new google.maps.Point(0, 0),
    new google.maps.Point(16, 35));

  lC = new google.maps.MarkerImage('https://oge.com/images/blueMarker.png',
    new google.maps.Size(20, 32),
    new google.maps.Point(0, 0),
    new google.maps.Point(16, 35));

  var person = prompt("lH, lN, or lC", "lC");
  var icon = lH;
  switch (person) {
    case "lH":
      icon = lH;
      break;
    case "lN":
      icon = lN;
      break;
    case "lC":
    default:
      icon = lC;
      break;
  }

  markere = new google.maps.Marker({
    position: new google.maps.LatLng(prompt("Latitude: ", "37.4419"), prompt("Longtitude: ", "-122.1419")),
    map: map,
    animation: google.maps.Animation.DROP,
    icon: icon,
    title: 'I am here'
  });

  google.maps.event.addListener(markere, 'mouseover', function() {
    this.setIcon(lH)
  });
  google.maps.event.addListener(markere, 'mouseout', function() {
    this.setIcon(lN)
  });
  google.maps.event.addListener(markere, 'mousedown', function() {
    this.setIcon(lC)
  });
  google.maps.event.addListener(markere, 'mouseup', function() {
    this.setIcon(lH)
  });
}
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>