title: Put a GeoJSON on a Leaflet map: Invalid GeoJSON object. throw new Error('Invalid GeoJSON object.');
body: <p>I've some trouble with a Leaflet code that build a GeoJSON and put it on the map.</p>
  
  <p>The GeoJSON is builded from an XML response of a service.</p>
  
  <p>The error is</p>
  
  <p><strong><em>Invalid GeoJSON object. throw new Error('Invalid GeoJSON object.');</em></strong></p>
  
  <p>My code is ... </p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Prova caricamento WMS Indirizzi del PCN&lt;/title&gt;
  &lt;meta charset="utf-8" /&gt;
  &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
      &lt;link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css"/&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="map" style="width: 600px; height: 400px"&gt;&lt;/div&gt;
  
      &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"&gt;&lt;/script&gt;   
      &lt;script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet-src.js"&gt;&lt;/script&gt;
  
  &lt;script&gt;
  
      var browser = navigator.userAgent.toLowerCase();
      var isIE = (browser.indexOf("msie")&gt;-1 || browser.indexOf("trident")&gt;-1);
  
  
      if (isIE &amp;&amp; window.XDomainRequest) {
                      if (window.XDomainRequest) {
                          var query = 'http://wms.pcn.minambiente.it/ogc?map=/ms_ogc/wfs/Numeri_Civici_2012.map&amp;SERVICE=WFS&amp;VERSION=1.0.0&amp;REQUEST=GetFeature&amp;TYPENAME=IN.NUMERICIVICI.2012&amp;SRSNAME=EPSG:4326&amp;bbox=7.719,44.849,7.72,44.85&amp;outputFormat=GML2';
                          var xdr = new XDomainRequest();
                          if (xdr) {
                              xdr.onload = function () {
                                  addressPCN(data);                               
                              }
                              xdr.onerror = function () { 
                                  alert("KO");
                              }
                              xdr.open('GET', query);
                              xdr.send();
                          }
                      }
                  }
                  else {
                      var query = 'http://wms.pcn.minambiente.it/ogc?map=/ms_ogc/wfs/Numeri_Civici_2012.map&amp;SERVICE=WFS&amp;VERSION=1.0.0&amp;REQUEST=GetFeature&amp;TYPENAME=IN.NUMERICIVICI.2012&amp;SRSNAME=EPSG:4326&amp;bbox=7.719,44.849,7.72,44.85&amp;outputFormat=GML2';
                      $.ajax({
                          type: "GET",
                          url: query,
                          dataType: "text",
                          crossDomain: true,
                          success: function (data) {
                                         addressPCN(data);                                
                          },
                          error: function (response, textStatus, errorThrown) {
                              alert("KO");
                              alert(errorThrown);
                          }
                      });
                  }   
  
  
      var map = L.map('map').setView([42, 12], 5);
  
      L.tileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', {
          maxZoom: 18,
          attribution: 'Map data &amp;copy; &lt;a href="http://openstreetmap.org"&gt;OpenStreetMap&lt;/a&gt; contributors, ' +
              '&lt;a href="http://creativecommons.org/licenses/by-sa/2.0/"&gt;CC-BY-SA&lt;/a&gt;, ' +
              'Imagery © &lt;a href="http://mapbox.com"&gt;Mapbox&lt;/a&gt;',
          id: 'examples.map-20v6611k'
      }).addTo(map);
  
              function addressPCN (addressList) {
  
               //alert("In function addressList");
               //alert(addressList);
  
                       var addressPCN = '{"type": "FeatureCollection","features":[';
                       $xmlData = $.parseXML(addressList);
                       $features = $('gml\\:featureMember, featureMember', $xmlData);
                       //alert($features.length);
                       $features.each(function () {
                                 var $this = $(this);
                                 //alert($this.find('ms\\:nome, nome').text() + ' - ' + $this.find('ms\\:civico, civico').text());
                                 addressPCN = addressPCN + '{"type": "Feature", "properties": { "popupContent": "' + $this.find('ms\\:nome, nome').text() + ' ' + $this.find('ms\\:civico, civico').text() + '", ' + '"show_on_map": true }, "geometry": { "type": "Point", "coordinates": [' + $this.find('gml\\:Point, Point').find('gml\\:coordinates, coordinates').text() + ']}},'
                       });                         
                       var addressPCN = addressPCN + ']}';
  
                       alert(addressPCN);
  
               function onEachFeature(feature, layer) {
                        if (feature.properties &amp;&amp; feature.properties.popupContent) {
                           layer.bindPopup(feature.properties.popupContent);
                        }
                       }
  
  
                       //var addressPCN = {"type": "FeatureCollection","features":[{"type": "Feature", "properties": { "popupContent": "Via Pegolo 9", "show_on_map": true }, "geometry": { "type": "Point", "coordinates": [7.719490,44.849197]}},{"type": "Feature", "properties": { "popupContent": "Via Pegolo 8", "show_on_map": true }, "geometry": { "type": "Point", "coordinates": [7.719490,44.849197]}},]};
               L.geoJson(addressPCN, {
                     onEachFeature: onEachFeature
               }).addTo(map);
              }
  
  &lt;/script&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>You can copy/paste it and shoud work on your desktop/laptop.</p>
  
  <p>Note that if you uncomment the row ....</p>
  
  <pre><code>//var addressPCN = {"type": "FeatureCollection","features":[{"type": "Feature", "properties": { "popupContent": "Via Pegolo 9", "show_on_map": true }, "geometry": { "type": "Point", "coordinates": [7.719490,44.849197]}},{"type": "Feature", "properties": { "popupContent": "Via Pegolo 8", "show_on_map": true }, "geometry": { "type": "Point", "coordinates": [7.719490,44.849197]}},]};
  </code></pre>
  
  <p>.... you've the GeoJSON I'm building (you can match it with the alert message ....). </p>
  
  <p>In this case all it's working right.</p>
  
  <p>NOTE: you've to use Firefox or Chrome at the momenti, my code still doesn't work foi IE.</p>
  
  <p>Any suggestion is appreciated!!</p>
  
  <p>Thank you in advance!!</p>
  
  <p>Cesare</p>