使用php解析xml,谷歌地图api v3标记

时间:2014-04-12 16:01:16

标签: javascript php parsing google-maps-api-3

我试图找到一种方法在php文件中使用函数initialize.i使用file_get_contents解析xml文件

$xml = file_get_contents('http://www.123.org/upload/123.xml');

    include 'xml_regex.php';
$news_items = element_set('item', $xml);
foreach($news_items as $item) {
    $description = value_in('description', $item);
    $glat = value_in( 'glat', $item );
    $glon = value_in( 'glon', $item );
    $item_array[] = array(
            'description' => $description,
            'glat'=>$glat,
            'glon'=>$glon
    );
  }

函数初始化

 </script>
    function initialize() {
              var mapOptions = {
                zoom: 6,
                center: new google.maps.LatLng( 38.822590,24.653320 )
              };
              var map = new google.maps.Map(document.getElementById('map-canvas'),
                  mapOptions);

            }
            google.maps.event.addDomListener(window, 'load', initialize);

                <?php foreach ( $item_array as $item ) {
                        //how can i use this lines inside the loop?
                        //var latlng      = new google.maps.LatLng(parseFloat("<?php $glat ?>"),
                                                                 //parseFloat("<?php $glon ?>"));
                        //alert (latlng);
                        //also inside the loop i wanna use the createMarker funciton

                  }?>

</script>

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

最后我找到了方法!只需引用代码。也许它会帮助将来的某个人!

<?php foreach ( $item_array as $item ) : ?>
                        var latlng = new google.maps.LatLng(parseFloat(<?php echo $item['glat']; ?>),
                                                            parseFloat(<?php echo $item['glon']; ?>));
                        if ( '<?php echo $item["temp"]; ?>' <= "1 °C" ) {
                             var marker = createMarker( latlng,cold );
                        }
                        function createMarker( latlng,cold ) {
                            var marker = new google.maps.Marker({
                            position: latlng,
                            map: map,
                            icon: cold
                            });
                            return marker;
                        }
相关问题