谷歌地图v3动态设置标记大小 [英] google maps v3 setting marker size dynamically

查看:108
本文介绍了谷歌地图v3动态设置标记大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我用ajax调用retreiving所有这些数据并将数据保存为对象,所以我可以检查标记的位置类型,并为该类型的位置放置适当的标记。



正在收集数据,但是当我尝试将数据添加到一个新的谷歌地图图像构造它的错误,我检查了一个警报,正确的信息在那里,这是怎么回事?

  //下面的代码将会出现问题!!! 
new google.maps.Size(32,37)//大小工程
//下面一行用动态值
new google.maps.Size(myIconArray.icon [myData .name_etp] .size)

完整代码:
静态代码行与存储在我正在创建的对象引用中的值相同)
http://www.focus-on-plants.com/locator_iconed.php

  function addMarker( map,myData){
console.log(myData)
//创建标记
//首先创建标记信息
myLatLng = new google.maps.LatLng(myData。 lat_mdt,myData.lng_mdt);
markersArray.push(myLatLng);
markersInfoArray.push(myData);

// ----------------------------
//现在创建实际图标
// alert(myIconArray.icon [myData.name_etp] .size)
$ b / * var iconimage = new google.maps.MarkerImage(
'/ images / icons / markers / flower_lb / image_med.png',//myIconArray.icon[myData.name_etp].image,
new google.maps.Size(myIconArray.icon [myData.name_etp] .size)//大小
//新的google.maps.Point(myIconArray.icon [myData.name_etp] .origin),//原点
// new google.maps.Point(myIconArray.icon [myData.name_etp] .anchorpoint)/ /锚点
); * /
//console.log (iconimage)
var iconshape = {
类型:'poly',
coord:[0, 2,1,2,1,1,18,0,18,1,19,2,20,19,19,19,18,21,14,21,10,26,5,21,1,21, 1,20,0,20,0,2]
}

//alert(myIconArray.shape[myData.name_etp])
mySize ='new google.maps。大小('+ myIconArray.icon [myData.name_etp] .size +')'
var iconi mage = new google.maps.MarkerImage(
myIconArray.icon [myData.name_etp] .image,
//以下代码有效!
new google.maps.Size(32,37)//大小可以工作
//下列换行符与动态值
// new google.maps.Size(myIconArray.icon [myData.name_etp] .size)


// new google.maps.Point(0,0),//原点
// new google.maps.Point (13,29)//锚点
);
var iconshadow = new google.maps.MarkerImage(
'/images/icons/markers/flower_lb/shadow_med.png',
google.maps.Size(42,18),/ /大小
新google.maps.Point(0,0),//原点
新google.maps.Point(13,18)//定位点
);

var marker = new google.maps.Marker({
position:myLatLng,
map:map,
title:myData.name_mdt,
icon :iconimage,
阴影:图标阴影//,
//形状:图标形状
});

//将匿名函数
中的事件监听器包装起来,我们立即调用并将变量i传递给它。
(function(myData,marker){
//创建事件侦听器,它现在可以访问
// myData和marker的值,因为它们是在创建期间
google.maps.event.addListener(marker,'click',function(){
//为infowindow创建内容
var content = createContent(myData);
var infowindow = new google .maps.InfoWindow({
content:content
});
infowindow.open(map,marker);
});
})(myData,marker );

};


解决方案

好,我找到了原因, p>

我正在传递新的google.maps.Size(myIconArray.icon [myData.name_etp] .size)



google image maps.size需要两个独立参数, new google.maps.Size(42,18)



我传递了 32,37 这实际上是一个字符串,



我需要使用以下分割字符串:

tmp = myIconArray.icon [myData.name_etp] .size.split(, )



然后为我使用的尺寸添加x和y: new google.maps.Size tmp [0],tmp [1])


heres the problem, i have a database thats hold information on marker images:

i'm retreiving all that with an ajax call and saving the data as objects, so i can check the location type of the marker and place the appropriate marker for that type of location.

am reciving the data fine but when i try to add the data into the contructor for a new google map image it errors, i have checked with an alert that the right information is there and it is so what gives???

// the following code will wor!!!
                   new google.maps.Size(32,37) // The Size works
//The following line breaks it with dynamic value    
                   new google.maps.Size(myIconArray.icon[myData.name_etp].size) 

heres the full code: (the test values in the static lines of code are the same values that are stored in the object references i am making) http://www.focus-on-plants.com/locator_iconed.php

function addMarker(map, myData){
      console.log(myData)
      // Create the marker
      // create the marker info first
      myLatLng = new google.maps.LatLng(myData.lat_mdt, myData.lng_mdt);
      markersArray.push(myLatLng);
      markersInfoArray.push(myData);

      // ----------------------------
      // NOW CREATE THE ACTUAL ICON
       //alert( myIconArray.icon[myData.name_etp].size)

      /*var iconimage = new google.maps.MarkerImage(
       '/images/icons/markers/flower_lb/image_med.png', //myIconArray.icon[myData.name_etp].image,
       new google.maps.Size(myIconArray.icon[myData.name_etp].size) // The Size
       //new google.maps.Point(myIconArray.icon[myData.name_etp].origin), // The origin
       //new google.maps.Point(myIconArray.icon[myData.name_etp].anchorpoint) // The anchor
      );*/
      //console.log(iconimage)
      var iconshape = {
       type: 'poly',
       coord: [0,2,1,2,1,1,18,0,18,1,19,2,20,19,19,19,18,21,14,21,10,26,5,21,1,21,1,20,0,20,0,2]
      }

      //alert(myIconArray.shape[myData.name_etp])
      mySize = 'new google.maps.Size('+myIconArray.icon[myData.name_etp].size+')'
      var iconimage = new google.maps.MarkerImage(
       myIconArray.icon[myData.name_etp].image,
       // the following code works!!!
                        new google.maps.Size(32,37) // The Size works
                       //The following line breaks iot with dynamic value    
                       //new google.maps.Size(myIconArray.icon[myData.name_etp].size) 


       //new google.maps.Point(0, 0), // The origin
       //new google.maps.Point(13,29) // The anchor
      );
      var iconshadow = new google.maps.MarkerImage(
       '/images/icons/markers/flower_lb/shadow_med.png',
       new google.maps.Size(42,18), // The Size
       new google.maps.Point(0,0), // The origin
       new google.maps.Point(13,18) // The anchor
      );

      var marker = new google.maps.Marker({
       position: myLatLng,
       map: map,
       title: myData.name_mdt,
       icon: iconimage,
       shadow: iconshadow//,
       //shape: iconshape
      });

      // Wrapping the event listener inside an anonymous function
      // that we immediately invoke and passes the variable i to.
      (function(myData, marker) {
       // Creating the event listener. It now has access to the values of
       // myData and marker as they were during its creation
       google.maps.event.addListener(marker, 'click', function() {
        //create thecontent for the infowindow
        var content = createContent(myData);
        var infowindow = new google.maps.InfoWindow({
         content: content
        });
        infowindow.open(map, marker);
       });
      })(myData, marker);  

     };

解决方案

ok so i found out why this was,

i was passing new google.maps.Size(myIconArray.icon[myData.name_etp].size)

google image maps.size takes two seperate parameters, new google.maps.Size(42,18)

i was passing 32, 37 which was actualy a string,

i needed to split the string up using the following:

tmp = myIconArray.icon[myData.name_etp].size.split(",")

then to add the x and y for the size i use: new google.maps.Size(tmp[0], tmp[1])

这篇关于谷歌地图v3动态设置标记大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆