【问题标题】:dropdown option for multiple marker icons with javascript带有 javascript 的多个标记图标的下拉选项
【发布时间】:2014-04-10 11:25:17
【问题描述】:

我很困惑用选项值更改我的自定义图标映射。所以请帮我处理你的cmets。 :D

这是选项值

      <select name='type' id='type' onChange='document.getElementById("customicon").value="this.value"'>
  <option<?php if($place['type'] == "sekolahremaja") {?> selected="selected"<?php } ?>>Sekolah Remaja</option>
  <option<?php if($place['type'] == "jalanremaja") {?> selected="selected"<?php } ?>>Jalan Remaja</option>
  <option<?php if($place['type'] == "komunitas") {?> selected="selected"<?php } ?>>Komunitas</option>
  <option<?php if($place['type'] == "screening") {?> selected="selected"<?php } ?>>Screening</option>
  <option<?php if($place['type'] == "lembaga") {?> selected="selected"<?php } ?>>Lembaga</option>
  <option<?php if($place['type'] == "personal") {?> selected="selected"<?php } ?>>Personal</option>
  <option<?php if($place['type'] == "foto") {?> selected="selected"<?php } ?>>Foto</option>
        </select>

这是我的 javascript 代码

var customicon = {
    "sekolahremaja" : "sekolahremaja",
    "jalanremaja" : "jalanremaja",
    "komunitas" : "komunitas",
    "screening" : "screening",
    "lembaga" : "lembaga",
    "personal" : "personal",
    "events" : "events",
    };
var markerImage = "../images/icons/"+customicon+".png";

          <?php if ($_GET['place_id']){?>
          var marker = new google.maps.Marker({
            position: new google.maps.LatLng(<?=$place['lat']?>,<?=$place['lng']?>),
            map: map,
            animation: google.maps.Animation.DROP,
            draggable: true,
            icon: markerImage
          });

【问题讨论】:

  • 也许你需要像var markerImage = "../images/icons/"+customicon[keyOfSelectedOption]+".png"; 这样的东西,因为customicon 是JSON 数据。无论如何#customicon 是什么?
  • #customicon 是我的选项值变量,我不知道是不是错了。我不知道如何使用javascript -_-

标签: javascript php google-maps drop-down-menu options


【解决方案1】:

试试这个

<select name='type' id='type' onChange="changeImage(this.value)">
  <option value="sekolahremaja" <?php if($place['type'] == "sekolahremaja") {?> selected="selected"<?php } ?>>Sekolah Remaja</option>
  <option value="jalanremaja" <?php if($place['type'] == "jalanremaja") {?> selected="selected"<?php } ?>>Jalan Remaja</option>
  <option value="komunitas" <?php if($place['type'] == "komunitas") {?> selected="selected"<?php } ?>>Komunitas</option>
  <option value="screening" <?php if($place['type'] == "screening") {?> selected="selected"<?php } ?>>Screening</option>
  <option value="lembaga" <?php if($place['type'] == "lembaga") {?> selected="selected"<?php } ?>>Lembaga</option>
  <option value="personal" <?php if($place['type'] == "personal") {?> selected="selected"<?php } ?>>Personal</option>
  <option value="foto" <?php if($place['type'] == "foto") {?> selected="selected"<?php } ?>>Foto</option>
        </select>

您的 javascript 如下所示

var markerImage='';
function changeImage(value)
{
  markerImage = "../images/icons/"+customicon.value+".png";
} 

因为 customicon 是对象

现在markerImage 拥有您想要显示为标记图标的图像网址

还检查position: new google.maps.LatLng(&lt;?=$place['lat']?&gt;,&lt;?=$place['lng']?&gt;) 在这里您需要将lat 和lng 转换为如下数字

 position: new google.maps.LatLng(Number(<?=$place['lat']?>),Number(<?=$place['lng']?>))

【讨论】:

    【解决方案2】:

    我不确定这是否是您要找的。​​p>

    <select name='type' id='type' onchange='changeMarker(this.value)'>
      <option value="sekolahremaja">Sekolah Remaja</option>
      ...
      <option value="foto">Foto</option>
    </select>
    

    javascript 的东西

    function changeMarker(v){
      var customicon = {
        "sekolahremaja" : "sekolahremaja",
        "jalanremaja" : "jalanremaja",
        "komunitas" : "komunitas",
        "screening" : "screening",
        "lembaga" : "lembaga",
        "personal" : "personal",
        "events" : "events",
        };
      var markerImage = "../images/icons/"+customicon[v]+".png";
      var marker = new google.maps.Marker({
                position: new google.maps.LatLng(<?=$place['lat']?>,<?=$place['lng']?>),
                map: map,
                animation: google.maps.Animation.DROP,
                draggable: true,
                icon: markerImage
              });
    }
    

    【讨论】:

    • 我不知道为什么,但它仍然不起作用。 :( 我以为我的另一个脚本有问题,但非常感谢 :) 我会把你的代码放在我的笔记上。 :)
    猜你喜欢
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 2018-09-06
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    相关资源
    最近更新 更多