简体   繁体   English

Google Maps API v3(PHP和Javascript)

[英]Google Maps API v3 (PHP & Javascript)

I would like to display multiple markers (up to 20 at least) on the Google maps via Javascript. 我想通过Javascript在Google地图上显示多个标记(至少20个)。 The data which comes in an array is in PHP. 数组中的数据在PHP中。

Upon running the code, the Google map only plots the last co-ordinates in the array. 运行代码后,Google地图只会绘制数组中的最后一个坐标。 Can you guys enlighten me why? 你们能启发我为什么吗? Following are the codes (Sorry if this is albeit messy as i'm a novice. thanks for any help in advance): 以下是代码(对不起,如果我还是新手,这很麻烦,谢谢。)。

$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); 
  }

Your code is little bit weird. 您的代码有点奇怪。 You have count($split_data) times included 'google maps library', closed tag </style> , created map , initialized function initialize . 您有count($split_data)次包含“谷歌地图库”,封闭标签</style> ,创建map ,初始化函数initialize Why? 为什么? Are you sure that count($split_data)>=20 ? 您确定count($split_data)>=20吗? Have you dumped it? 丢了吗 Why don't separate php-part and js-part? 为什么不分开php-part和js-part? You could get all places to be displayed on the map using ajax, or you could even from needed array of obects(each object could contain lat , lng , name , etc) in php. 您可以使用ajax将所有地点显示在地图上,或者甚至可以从php中所需的对象数组(每个对象可以包含latlngname等)中获取。 Then make $j_array=json_encode($array) in php-part to convert it to string and var places=<?php echo $j_array; ?>; 然后在php-part中使$j_array=json_encode($array)转换为字符串和var places=<?php echo $j_array; ?>; var places=<?php echo $j_array; ?>; in js-part to make object again. 在js-part中再次制作对象。 And than you are to work with array of objects in js. 而且比在js中处理对象数组还要多。 It is much easier and obviously. 这显然容易得多。 Refactor your code. 重构您的代码。 It is messed. 糟透了。

I hope it will be helpfull. 希望对您有所帮助。 Sorry for messed english, I treid to be clear. 对不起,英语不好,我很清楚。

You doesn't need to loop the full js part (including <script> initialize ). 您不需要循环整个js部分(包括<script> initialize )。 You get only the last value because of that. 因此,您只会得到最后一个值。 Try something like this instead... 尝试这样的事情...

// 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM