2011-12-29 2 views
1

私はJQueryの.get関数を使用してXMLファイルを取得しています。ファイルの値を読み取ろうとすると、すべての変数は空になります。ここでJQuery XMLの読み込み - 空の変数

は、私が取得し、XMLファイルを読んでいる機能である:

var get_locations = function(neLat, neLng, swLat, swLng, map, infoWindow) 
     { 
      $.get('Scripts/googlemap_ajax.php', {neLat: neLat, neLng: neLng, swLat: swLat, swLng: swLng}, 
      function(xml) 
      { 
       $(xml).find("marker").each(function() 
       { 
        var name = $(this).find('name').text(); 
        var tags = $(this).find('tags').text(); 
        var coupon = $(this).find('coupon').text(); 
        var icon = customIcons[coupon] || {}; 
        var point = new google.maps.LatLng(
           $(this).find('latitude').text(), 
           $(this).find('longitude').text()); 
     alert($(this).find('name').text()); 
        var marker = new google.maps.Marker({ 
         position: point, 
         map: map, 
         icon: icon.icon 
        }); 


        var html = "HELLO THERE"; 

        google.maps.event.addListener(marker, 'click', function() { 
          infoWindow.setContent(html); 
          infoWindow.open(map, marker); 
        }); 
       }); 
      }, "xml"); 
     }; 

コードが実行されると、私は$(this).find('name').text()の値が空であることを警告を取得します。ここで

は(Firebugのによって検証)対応するXMLファイルである:ここで

<markers> 
<marker locationID="1" name="Chris' Food Place" tags="American, Sushi" latitude="34.113432" longitude="-117.169855" coupon="yes"></marker> 
<marker locationID="2" name="Chris' Food Place 2" tags="American," latitude="34.1158092" longitude="-117.1704145" coupon="no"></marker> 
</markers> 

は誰が問題を引き起こしている何かを見つけるために起こるだけの場合には、コードのブロック全体である:

<style type="text/css" > 

    html { height: 100% } 

    body { height: 100%; margin: 0; padding: 0 } 

</style> 



<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script> 
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script> 


<script type="text/javascript"> 

$(document).ready(function() 
{ 
    var alerted = false; 

    function MYMAP() 
    { 
     //internal 
     var map = ''; 
     var center = ''; 
     var centerImage = '../Images/map_icon.png'; 
     var infoWindow = ''; 
     var last_ne_lat; //Check previous map corner 

     var customIcons = { 
       yes: { 
       icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png', 
       shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png' 
       }, 
       no: { 
       icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png', 
       shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png' 
       } 
     }; 

     var get_locations = function(neLat, neLng, swLat, swLng, map, infoWindow) 
     { 
      $.get('Scripts/googlemap_ajax.php', {neLat: neLat, neLng: neLng, swLat: swLat, swLng: swLng}, 
      function(xml) 
      { 
       $(xml).find("marker").each(function() 
       { 
        var name = $(this).find('name').text(); 
        var tags = $(this).find('tags').text(); 
        var coupon = $(this).find('coupon').text(); 
        var icon = customIcons[coupon] || {}; 
        var point = new google.maps.LatLng(
           $(this).find('latitude').text(), 
           $(this).find('longitude').text()); 
     alert($(this).find('name').text()); 
        var marker = new google.maps.Marker({ 
         position: point, 
         map: map, 
         icon: icon.icon 
        }); 


        var html = "HELLO THERE"; 

        google.maps.event.addListener(marker, 'click', function() { 
          infoWindow.setContent(html); 
          infoWindow.open(map, marker); 
        }); 
       }); 
      }, "xml"); 
     }; 


    //external: 
    return { 
      init: function(selector, latLng, zoom) 
      { 
       var myOptions = { 
         zoom:zoom, 
         center: latLng, 
        streetViewControl: false, 
         mapTypeId: google.maps.MapTypeId.ROADMAP 
       } 
       this.map = new google.maps.Map($(selector)[0], myOptions); 
      }, 

      centerMarker: function(latlng) 
      { 
       this.center = new google.maps.Marker({ 

       position: latlng, 

       map: this.map, 

       icon: centerImage, 

       title: "You are here" 

       }); 
      }, 

      addBoundChange: function() 
      { 
       this.infoWindow = new google.maps.InfoWindow; 
       test = this.map; 
       test2 = this.infoWindow; 
       // Add listener to map 
       google.maps.event.addListener(this.map, 'bounds_changed', function() { 
        var zoom_level = this.getZoom(); 
        if(zoom_level > 12) 
        { 
         var bounds = this.getBounds(); 
         var ne = bounds.getNorthEast(); 
         var neLat = bounds.getNorthEast().lat(); 
         var neLng = bounds.getNorthEast().lng(); 
           var sw = bounds.getSouthWest(); 
         var swLat = bounds.getSouthWest().lat(); 
         var swLng = bounds.getSouthWest().lng(); 
         if(neLat != last_ne_lat) 
         { 
          last_ne_lat = neLat;  
          get_locations(neLat, neLng, swLat, swLng, test, test2); 

         } 
        } 
        else //Alerts the user only once. 
        { 
         if(!alerted)  
         { 
          alerted=true; 
          alert("Please zoom in to continue displaying the location markers"); 
         } 
        } 
       }); 
      } 

     }; 
    } 
    var testMap = new MYMAP(); 
    var latlng = new google.maps.LatLng(<?php echo $lat; ?>,<?php echo $long; ?>); 
    testMap.init('#map_canvas', latlng, 16); 
    testMap.centerMarker(latlng); 
    testMap.addBoundChange(); 

}); 
</script> 


<body> 
    <div id="locationSelect" style="width:100%"></div> 

    <div id="map_canvas" style="width:70%; height:90%"></div> 

</body> 

答えて

0

ターゲットはタグではなく属性です。 $(この)の代わりに$(この)の.ATTR( "名前") .find( '名前')を試してみてください。テキスト()

0

.find(セレクタ)は、それぞれの子孫を横断するために使用されていますマッチした要素

.ATTR(attributeNameの)の現在のセット内の要素は、XMLの属性にアクセスしようとしているマッチした要素

のセットの最初の要素の属性の値を取得するために使用されます素子。 $(this).attr(attributeName)を使用して値を取得します。

関連する問題