Getting XML from a PHP API in Objective-C for use with Google Maps SDK



I am currently utilising the following PHP script to create an XML output from a MySQL database which enables me utilise the output to create markers on a Google Map instance.



<?php
require("phpsqlajax_dbinfo.php");

function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','&lt;',$htmlStr);
$xmlStr=str_replace('>','&gt;',$xmlStr);
$xmlStr=str_replace('"','&quot;',$xmlStr);
$xmlStr=str_replace("'",'&#39;',$xmlStr);
$xmlStr=str_replace("&",'&amp;',$xmlStr);
return $xmlStr;
}

$connection=mysql_connect ('stefanbatterbeecom1.ipagemysql.com', $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}

$query = "SELECT * FROM reports WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}

header("Content-type: text/xml");

// Start XML file, echo parent node
echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'report_id="' . parseToXML($row['report_id']) . '" ';
echo 'signal="' . parseToXML($row['signal']) . '" ';
echo 'latitude="' . $row['latitude'] . '" ';
echo 'longitude="' . $row['longitude'] . '" ';
echo 'vendor="' . $row['vendor'] . '" ';
echo 'date="' . $row['date'] . '" ';
echo '/>';
}

// End XML file
echo '</markers>';

?>


It generates an XML output like the following:



<markers>
<marker report_id="44" signal="17" latitude="51.544563" longitude="-2.568260" vendor="EE" date="2014-11-20 16:47:04 0000"/>
<marker report_id="45" signal="19" latitude="51.544575" longitude="-2.568338" vendor="EE" date="2014-11-20 16:53:17 0000"/>
<marker report_id="46" signal="80" latitude="51.544548" longitude="-2.568370" vendor="vodafone UK" date="2014-11-20 16:55:10 0000"/>
<marker report_id="47" signal="63.4" latitude="51.544544" longitude="-2.568280" vendor="vodafone UK" date="2014-11-20 17:01:41 0000"/>
<marker report_id="48" signal="71.92" latitude="51.544575" longitude="-2.568365" vendor="vodafone UK" date="2014-11-20 17:01:50 0000"/>
<marker report_id="49" signal="72.4" latitude="51.544567" longitude="-2.568403" vendor="vodafone UK" date="2014-11-20 17:01:57 0000"/>
<marker report_id="50" signal="67.48" latitude="51.544537" longitude="-2.568335" vendor="vodafone UK" date="2014-11-20 17:02:06 0000"/>
<marker report_id="51" signal="19" latitude="51.539841" longitude="-2.563924" vendor="EE" date="2014-11-20 17:04:28 0000"/>
<marker report_id="52" signal="17" latitude="51.544533" longitude="-2.568224" vendor="EE" date="2014-11-20 17:04:55 0000"/>
<marker report_id="53" signal="17.88" latitude="51.539551" longitude="-2.563656" vendor="EE" date="2014-11-21 11:28:38 0000"/>
<marker report_id="54" signal="17" latitude="51.544552" longitude="-2.568236" vendor="EE" date="2014-11-21 11:28:50 0000"/>
</markers>


I can then use the longitude/latitude from this XML output to add the markers to Google Maps on a front facing HTML page. Then, I access this HTML page with a UIWebView within the application.


I'm almost positive that this isn't the most efficient/best way to do this, but it's the only method I could find for creating markers from an SQL database. If there's a more efficient method, please say.


My question is, I now want to use the Google Maps SDK for iOS & somehow add markers to an iOS version of the map from my SQL database. How can I access the objects of the XML output from the PHP file to create markers on a Google Maps for iOS instance?


No comments:

Post a Comment