Google地图:关于标记图标和地理编码的问题 [英] Google Maps : Issue regarding marker icons and geocoding

查看:97
本文介绍了Google地图:关于标记图标和地理编码的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个jsp,我已经使用Google地图中的地理编码器类将地址转换为标记。现在我唯一遇到的问题是,当我试图更改标记的图标图像时,我只获得了所有标记的最后一个图标。

 < script type =text / javascript> 
var geocoder;
var address = new Array();
var marker = new Array();
var images = [A,B,C,D,E,F,G,H,I,J,K , L];

函数initialize()
{
var locations = new String(<%= request.getParameter(location)%>);
locations = locations.substring(0,(locations.length)-1);

var map = new google.maps.Map(document.getElementById('googleMap'),{
mapTypeId:google.maps.MapTypeId.ROADMAP,
zoom:8
});

if(locations =='null'|| locations =='')
locations ='Pune,INDIA \;';

address = locations.split('\;');

marker [0] = new google.maps.Marker({
map:map
});

geocoder = new google.maps.Geocoder();
var i = 1;
for(i = 1; i< address.length; i ++){
geocoder.geocode({
'address':address [i] +,印度
} ,function(results,status){
if(status == google.maps.GeocoderStatus.OK){
var marker = new google.maps.Marker({
map:map,
图标:'http://www.google.com/mapfiles/marker'+images[i]+'.png',
位置:结果[0] .geometry.location
} );
var info = new google.maps.InfoWindow({
content:address [i] +',India'
});
google.maps.event.addListener (marker,'click',function(){
info.open(map,marker);
});
}
});

geocoder.geocode(
'address':address [0] +,India
},function(results,status){
if(状态== google.maps.GeocoderStatus.OK){
marker [0] .setPosition(results [0] .geometry.location);
map.setCenter(results [0] .geometry.location) ;
new google.maps.Circle({
center:results [0] .geometry.location,
radius:radius * 1000,//转换为米
fillColor:' #00ff00',
fillOpacity:0.2,
map:map,
strokeColor:'#0000ff',
strokeOpacity:0.3,
strokeWeight:1
});
}
});
} //初始化函数结束。
google.maps.event.addDomListener(window,'load',initialize);
< / script>

请帮忙!!!

解决方案

这是一个常见的问题(通常遇到infowindows),它可以通过函数闭包来解决(一个createMarker函数是一个选项)。

 函数createMarker(地图,latlng,地址,图片){
var marker = new google.maps.Marker({
map:map,
图标:'http://www.google.com/mapfiles/marker'+image+'.png',
位置:latlng
});
var info = new google.maps.InfoWindow({
content:address +',India'
});
google.maps.event.addListener(marker,'click',function(){
info.open(map,marker);
});
返回标记;

$ / code>

在您的循环中调用它,如下所示:

  geocoder.geocode({
'address':address [i] +,印度
,函数(结果,状态) {
if(status == google.maps.GeocoderStatus.OK){
var marker = createMarker(results [0] .geometry.location,address [i],images [i]);
}
});

工作示例(您发布的代码不像您所描述的那样,包括地理编码操作的函数关闭)

更新:
您还需要地址上的函数关闭(geocodeAddress函数):

  function geocodeAddress(map,address,i){
geocoder.geocode({
'address':address +,India
},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
var marker = createMarker(map,results [0] .geometry.location,address,images [i]);
}
});

$ / code>

所以你的处理循环变成:

  var i = 1; 
for(i = 0; i< address.length; i ++){
geocodeAddress(map,address [i],i);

程式码片段:



  var geocoder; var address = new Array(); var marker = new Array(); var images = [A,B,C, D,E,F,G,H,I,J,K,L, Mumbai\;'; // locations = locations.substring(0,(locations.length)-1); var map = new google.maps.Map(document.getElementById('googleMap'),{mapTypeId:google.maps.MapTypeId.ROADMAP,zoom:8}); if(locations =='null'|| locations =='')locations ='Pune,INDIA \;'; address = locations.split('\;'); marker [0] = new google.maps.Marker({map:map}); geocoder = new google.maps.Geocoder(); var i = 1;对于(i = 0; i  

  html,body,#googleMap {height:100%; width:100%;}  

< script src = https://maps.googleapis.com/maps/api/js\"></script><div id =googleMap>< / div>


I have created a jsp where i have converted the addresses into markers using geocoder class in google maps. Now the only problem i'm facing is that when i'm trying to change icon image for the marker, i'm only getting last icon for all the marker.

<script type="text/javascript">
var geocoder;  
var address = new Array();  
var marker = new Array();  
var images=["A","B","C","D","E","F","G","H","I","J","K","L"];  

function initialize()
{
var locations = new String("<%=request.getParameter("location")%>");  
locations=locations.substring(0,(locations.length)-1); 

var map = new google.maps.Map(document.getElementById('googleMap'), { 
   mapTypeId: google.maps.MapTypeId.ROADMAP,
   zoom: 8
});

if(locations=='null' || locations=='')
    locations = 'Pune, INDIA\;';

address = locations.split('\;');

marker[0] = new google.maps.Marker({
    map:map 
});

geocoder = new google.maps.Geocoder();
var i=1;
for(i=1; i<address.length; i++){
    geocoder.geocode({
          'address': address[i]+", India"
       },function(results, status){
          if(status == google.maps.GeocoderStatus.OK) {
            var marker = new google.maps.Marker({
                map: map,  
                icon: 'http://www.google.com/mapfiles/marker'+images[i]+'.png',
                position: results[0].geometry.location
            });
            var info = new google.maps.InfoWindow({
                content: address[i]+', India'
            });
            google.maps.event.addListener(marker, 'click', function() {
                info.open(map, marker);
            });
          }
       });
}
geocoder.geocode({
    'address': address[0]+", India"
    },function(results, status){
      if(status == google.maps.GeocoderStatus.OK) {
         marker[0].setPosition(results[0].geometry.location);
         map.setCenter(results[0].geometry.location);
         new google.maps.Circle({
             center: results[0].geometry.location,
             radius: radius * 1000,       // Convert to meters
             fillColor: '#00ff00',
             fillOpacity: 0.2,
             map: map,
             strokeColor : '#0000ff',
             strokeOpacity : 0.3,
             strokeWeight: 1
        });
     }
});
}//End of initialize function.  
google.maps.event.addDomListener(window, 'load', initialize);
</script>

Please help !!!

解决方案

This is a common problem (usually encountered with infowindows), it can be solved with function closure (a "createMarker" function is one option).

function createMarker(map, latlng, address, image) {
        var marker = new google.maps.Marker({
            map: map,  
            icon: 'http://www.google.com/mapfiles/marker'+image+'.png',
            position: latlng
        });
        var info = new google.maps.InfoWindow({
            content: address+', India'
        });
        google.maps.event.addListener(marker, 'click', function() {
            info.open(map, marker);
        });
  return marker;
}

call it inside your loop like this:

geocoder.geocode({
      'address': address[i]+", India"
   },function(results, status){
      if(status == google.maps.GeocoderStatus.OK) {
        var marker = createMarker(results[0].geometry.location, address[i], images[i]);
      }
   });

working example (the code you posted didn't behave as you described, includes function closure for the geocoding operation as well)

UPDATE: You also need function closure on the address (a "geocodeAddress" function):

function geocodeAddress(map, address, i) {
   geocoder.geocode({
      'address': address+", India"
   },function(results, status){
      if(status == google.maps.GeocoderStatus.OK) {
    var marker = createMarker(map, results[0].geometry.location, address, images[i]);
      }
   });
}

So your processing loop becomes:

var i=1;
for(i=0; i<address.length; i++){
  geocodeAddress(map, address[i], i);
}

code snippet:

var geocoder;
var address = new Array();
var marker = new Array();
var images = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"];

function initialize() {
    var locations = 'Pune\;Mumbai\;';
    // locations=locations.substring(0,(locations.length)-1); 

    var map = new google.maps.Map(document.getElementById('googleMap'), {
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      zoom: 8
    });

    if (locations == 'null' || locations == '')
      locations = 'Pune, INDIA\;';

    address = locations.split('\;');

    marker[0] = new google.maps.Marker({
      map: map
    });

    geocoder = new google.maps.Geocoder();
    var i = 1;
    for (i = 0; i < address.length; i++) {
      geocodeAddress(map, address[i], i);
    }

    geocoder.geocode({
      'address': address[0] + ", India"
    }, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        //         marker[0].setPosition(results[0].geometry.location);
        map.setCenter(results[0].geometry.location);
        new google.maps.Circle({
          center: results[0].geometry.location,
          radius: 1000, // radius * 1000,       // Convert to meters
          fillColor: '#00ff00',
          fillOpacity: 0.2,
          map: map,
          strokeColor: '#0000ff',
          strokeOpacity: 0.3,
          strokeWeight: 1
        });
      }
    });
  } //End of initialize function.  

function geocodeAddress(map, address, i) {
  geocoder.geocode({
    'address': address + ", India"
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      var marker = createMarker(map, results[0].geometry.location, address, images[i]);
    }
  });
}

function createMarker(map, latlng, address, image) {
  var marker = new google.maps.Marker({
    map: map,
    icon: 'http://www.google.com/mapfiles/marker' + image + '.png',
    position: latlng
  });
  var info = new google.maps.InfoWindow({
    content: address + ', India'
  });
  google.maps.event.addListener(marker, 'click', function() {
    info.open(map, marker);
  });
  return marker;
}
google.maps.event.addDomListener(window, 'load', initialize);

html,
body,
#googleMap {
  height: 100%;
  width: 100%;
}

<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="googleMap"></div>

这篇关于Google地图:关于标记图标和地理编码的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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