2012-04-12 13 views
-1

を呼び出す:GoogleマップAPIの実装。これは、以下の私のGoogleマップコードである

<div id="map_canvas" style="width: 500px; height: 400px;"></div> 

<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> 
<script type="text/javascript"> 
InitMap("[['Leslie Fleming',41.977, -71.3248, 1 ],['Chris Reale',41.977, -71.3248, 2 ],['Michael Anello',41.977, -71.3248, 3 ],['Ed Pariseau Jr',41.977, -71.3248, 4 ],['Ryan Higgens',41.977, -71.3248, 5 ]]", "41.977, -71.3248"); 
function InitMap(MapElement, MapPointer){ 
    var locations = MapElement; 
    var map = new google.maps.Map(document.getElementById('map_canvas'), { 
     zoom: 10, 
     center: new google.maps.LatLng(MapPointer), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

    var infowindow = new google.maps.InfoWindow(); 

    var marker, i; 

    for (i = 0; i < locations.length; i++) { 
     marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
     map: map 
     }); 

     google.maps.event.addListener(marker, 'click', (function(marker, i) { 
     return function() { 
      infowindow.setContent(locations[i][0]); 
      infowindow.open(map, marker); 
     } 
     })(marker, i)); 
    } 
} 
</script> 

それが動作しないのはなぜ?私は間違って何かしていますか?

+0

「機能しない」とは何ですか?理由を見つけるためにデバッガを使いましたか? –

+0

ハードコードを使用すると動作します –

+0

投票しないでください。もし誰かがエラーを見つけたら、それ以外の場合は教えてください –

答えて

0

私が解決したコードを参照してください。これは複数のGoogleマップのマーカーです。あなたはAJAXを通してそれを使うことができます。私はプログラマーを雇う必要はありません。私はそれを解決することができます。

<div id="map_canvas" style="width: 500px; height: 400px;"></div> 

<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> 
<script type="text/javascript"> 
InitMap([["Leslie Fleming",41.977, -71.3248, 1 ],["Chris Reale",41.977, -71.3248, 2 ],["Michael Anello",41.977, -71.3248, 3 ],["Ed Pariseau Jr",41.977, -71.3248, 4 ],["Ryan Higgens",41.977, -71.3248, 5 ]], 41.977, -71.3248); 
function InitMap(MapElement, latitude, longitude){ 
    var locations = MapElement; 
    var map = new google.maps.Map(document.getElementById('map_canvas'), { 
     zoom: 10, 
     center: new google.maps.LatLng(latitude, longitude), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

    var infowindow = new google.maps.InfoWindow(); 

    var marker, i; 

    for (i = 0; i < locations.length; i++) { 
     marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
     map: map 
     }); 

     google.maps.event.addListener(marker, 'click', (function(marker, i) { 
     return function() { 
      infowindow.setContent(locations[i][0]); 
      infowindow.open(map, marker); 
     } 
     })(marker, i)); 
    } 
} 
</script> 
-3

私が解決したコードを参照してください。これは複数のGoogleマップのマーカーです。あなたはAJAXを通してそれを使うことができます。私はプログラマーを雇う必要はありません。私はそれを解決することができます。

<div id="map_canvas" style="width: 500px; height: 400px;"></div> 

<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> 
<script type="text/javascript"> 
InitMap([["Leslie Fleming",41.977, -71.3248, 1 ],["Chris Reale",41.977, -71.3248, 2 ],["Michael Anello",41.977, -71.3248, 3 ],["Ed Pariseau Jr",41.977, -71.3248, 4 ],["Ryan Higgens",41.977, -71.3248, 5 ]], 41.977, -71.3248); 
function InitMap(MapElement, latitude, longitude){ 
    var locations = MapElement; 
    var map = new google.maps.Map(document.getElementById('map_canvas'), { 
     zoom: 10, 
     center: new google.maps.LatLng(latitude, longitude), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

    var infowindow = new google.maps.InfoWindow(); 

    var marker, i; 

    for (i = 0; i < locations.length; i++) { 
     marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
     map: map 
     }); 

     google.maps.event.addListener(marker, 'click', (function(marker, i) { 
     return function() { 
      infowindow.setContent(locations[i][0]); 
      infowindow.open(map, marker); 
     } 
     })(marker, i)); 
    } 
} 
</script> 
+0

あなたが本当に必要なのは、最初の要素を引用符(文字列にする)を残して配列にすることだけでした。そして、あなたが望む配列を得るために文字列を 'eval'する必要はありません。元のコードで、 'InitMap([['Leslie Fleming'、41.977、-71.3248、1]、[...] ...])関数を呼び出すと、最初の引数は配列で、文字列ではありません。 'eval'を使うのに-1を使います。 –

+0

正確に。私はこのタイプの答えを期待していました。ありがとう、あなたの考えと私のコードは両方とも動作しています:) –

関連する問題