I'm using the Google Maps API to make an datavisualisation that shows locations from an rss feed. I've managed to get the map and rss location working, but I'm struggling to get the locations in the map.
This is an excerpt of the feed:
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Alarmeringen.nl feed</title>
<link>http://alarmeringen.nl</link>
<description>Alarmeringen.nl: Alle alarmeringen voor: Amsterdam-Amstelland</description>
<atom:link href="http://alarmeringen.nl/feeds/region/amsterdam-amstelland/brandweer.rss"
rel="self"/>
<language>nl-NL</language>
<lastBuildDate>Wed, 11 Jun 2014 16:46:42 +0200</lastBuildDate>
<item>
<title>HV 2 LIFTOPSLUITING (+Inc.net: Reg.Inmeld+) , Talbotstraat 47 AMSTERDAM [ ASZ
]</title>
<link>http://alarmeringen.nl/amsterdam-amstelland/amsterdam/15757258/p2000-assistentie-bij-liftopsluiting-op-talbotstraat-in-amsterdam-brandweer-hulpverlening-ter-plaatse.html</link>
<description>Assistentie bij liftopsluiting op Talbotstraat in Amsterdam, brandweer
hulpverlening ter plaatse</description>
<pubDate>Wed, 11 Jun 2014 16:46:42 +0200</pubDate>
<guid>d5ae65f5049871959f15130ceb06bf2f</guid>
</item>
<!-- .. many more items ... -->
<item>
<title>BR 1 BRAND WONING (+Inc.net: 2+) (KEUKEN) (WOONHUIS) (SLACHTOFFERS: ) ,
Anfieldroad 228 AMSTERDAM [ OVDN VVD ]</title>
<link>http://alarmeringen.nl/amsterdam-amstelland/amsterdam/15755472/p2000-woningbrand-op-anfieldroad-in-amsterdam-brandweer-met-spoed-ter-plaatse.html</link>
<description>Woningbrand op Anfieldroad in Amsterdam, brandweer met spoed ter
plaatse</description>
<pubDate>Wed, 11 Jun 2014 13:28:07 +0200</pubDate>
<guid>ad9a2c0586db794415588e2b4e50375a</guid>
</item>
</channel>
</rss>
My PHP code for the locations:
$xml = simplexml_load_file('http://alarmeringen.nl/feeds/region/amsterdam-amstelland/brandweer.rss');
$titles = $xml->xpath('//title');
foreach($titles as $title) {
if(isset(explode(' [', explode(', ', $title)[1])[0]))
{
echo "Found " . explode(' [', explode(', ', $title)[1])[0] . "<br />";
}
}
But now I need to get the values that come out of the foreach
loop into the address
variable in JavaScript:
var address = 'Talbotstraat 47 AMSTERDAM';
UPDATE
This is the JavaScript code I use to show an address on the map:
function initialize()
{
geocoder = new google.maps.Geocoder();
var myLatlng = new google.maps.LatLng(52.37022, 4.89517);
var mapOptions = {
zoom: 11,
center: myLatlng,
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
if (geocoder)
{
geocoder.geocode(
{
'address': address // The variable is used here
},
function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
if (status != google.maps.GeocoderStatus.ZERO_RESULTS)
{
map.setCenter(results[0].geometry.location);
var infowindow = new google.maps.InfoWindow(
{
content: '<b>' + address + '</b>',
size: new google.maps.Size(150, 50)
});
var marker = new google.maps.Marker(
{
position: results[0].geometry.location,
map: map,
title:address
});
google.maps.event.addListener(marker, 'click', function()
{
infowindow.open(map,marker);
});
}
else
{
alert('No results found');
}
}
else
{
alert('Geocode was not successful for the following reason: ' + status);
}
}
);
}
}