2011-07-28 77 views
0

我想通過Javascript在Google地圖上顯示多個標記(至少20個)。數組中的數據在PHP中。Google Maps API v3(PHP&Javascript)

運行代碼時,Google地圖只繪製數組中最後一個座標。你們能告訴我爲什麼嗎?以下是代碼(很抱歉,如果這是雖然凌亂我是一個新手感謝提前任何幫助。):

$link ="http://network-tools.com/default.asp?prog=trace&host="."$ip_address"; 
$link_traceroute ="http://api.ipinfodb.com/v3/ip-city/?key=a15e8640c34837e4d402df55d7fd5e059e50d0d407d285a7a3b2ccbf85e1a234&ip="; 

$response = file_get_contents("$link", false, $context); 

$pieces_traceroute = strchr ($response, "$ip_address is from"); 
$split_pieces_traceroute = str_replace("Trace","$$$",$pieces_traceroute); 
$better_pieces_traceroute =(explode("$$$",$split_pieces_traceroute)); 

$raw_data = strip_tags($better_pieces_traceroute[1]); 
$split_data = (explode(" ",$raw_data)); 

for ($i=0; $i<count($split_data);$i++) 
{ 

$checker= valid_ip($split_data[$i]); 

    if ($checker != null){ 
    $response_traceroute = file_get_contents("$link_traceroute"."$split_data[$i]", false, $context); 
    $pieces_traceroute = (explode(";",$response_traceroute)); 
    $Cord1 = $pieces_traceroute[8]; 
    $Cord2 = $pieces_traceroute[9]; 
    echo $Cord1.nl2br("\n"); 
    echo $Cord2.nl2br("\n"); 
    ?> 

    </style> 
<script type="text/javascript" 
    src="http://maps.googleapis.com/maps/api/js?sensor=true"> 
</script> 
<script type="text/javascript"> 
    function initialize() { 
    var myLatlng = new google.maps.LatLng(<?php echo $Cord1;?>, <?php echo $Cord2;?>); 
    var myOptions = { 
    zoom: 4, 
    center: myLatlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    } 
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

var marker = new google.maps.Marker({ 
     position: myLatlng, 
     title:"Hello World!" 
    }); 

    // To add the marker to the map, call setMap(); 
    marker.setMap(map); 
    } 
+0

你可以轉儲/打印出數組? – toopay

+0

這裏你去:) ERM以前要求以下是我的結果是我的回聲協調一致。它們來自變量$ Cord1和$ Cord2。 38.9048 -77.0354 38.9048 -77.0354 38.9048 -77.0354 44.9631 -93.4943 38.9048 -77.0354 38.9048 -77.0354 38.9048 -77.0354 38.9048 -77.0354 38.9048 -77.0354 38.9048 -77.0354 39.9 116.413 39.9 116.413 39.9 116.413 39.9 116.413 29 120 –

回答

0

您不需要循環完整的js部分(包括<script>initialize)。因爲這個,你只能得到最後一個值。嘗試這樣的代替...

// Assume we have $locations variable which hold array data 
<script> 
function initialize() { 
    var centerMap = <?php echo json_encode($locations[0]) ?>; 
    var locations = <?php echo json_encode($locations) ?>; 
    var centerLatlng = new google.maps.LatLng(centerMap[0], centerMap[1]); 
    var map = new google.maps.Map(document.getElementById("map_canvas"), { 
     zoom: 4, 
     center: centerLatlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
    }); 

    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 
     }); 
    } 
} 
</script> 
+0

嗯,好吧,我編輯了你的代碼,並使其工作。我可以知道你如何顯示標記生活嗎?因爲它會繪製標記一次,並刷新瀏覽器和on? –

+0

上面的代碼是將你的php數組轉換成json結構(通過這一行:'var locations = <?php echo json_encode($ locations)?>;'),所以在js中使用它更容易。然後你循環json,而不是在循環語句中循環你的php數組(這是標記製作/繪製並附加到你的地圖,由行開始for(i = 0; i toopay

0

你的代碼是有點怪異。您有count($split_data)次包括'谷歌地圖庫',封閉標籤</style>,創建map,初始化函數initialize。爲什麼?你確定count($split_data)>=20?你甩了它嗎?爲什麼不分開php-part和js-part?您可以使用ajax將所有地點顯示在地圖上,或者甚至可以根據需要的數組元素(每個對象可以包含lat,lng,等)。然後在php-part中將$j_array=json_encode($array)轉換爲字符串,並在js-part中將其轉換爲var places=<?php echo $j_array; ?>;以再次創建對象。而且你要在js中處理一些對象。這很容易,很明顯。重構你的代碼。它是混亂的。

我希望它會有幫助。對不起,英語搞砸了,我很清楚。

+0

呃座標的數i有是動態的。它不一定是20.我硬編碼它,以檢查谷歌地圖是否會遍歷我的數組,並繪製它們顯然沒有。呃,我不熟悉ajax,這個代碼是Google提供的示例。我只是將它集成到我的PHP數組編碼中,以便繪製各種座標。不知道我是否回答你的問題,但雅:( –