Jump to content

Php / Javascript Error Using Rest Web Service And Google Maps Api


skaterdav85
 Share

Recommended Posts

So Im using this one REST web service, and in the XML data, it has latitude and longitude points. I am taking these values, and echoing them into a JavaScript array, which the Google Maps API will then use to plot the points. However, in the XML data, sometimes there wont be a latitude or longitude value. Instead of being:<latitude>34.34343</latitude><longitude>-120.34646</longitude>it will sometimes be like:<latitude/>so when my PHP is done processing, my JS array will have holes in it like this:lat[0]=34.0627717; lon[0]=-117.2082592; lat[1]=42.991484; lon[1]=-83.652923; lat[2]=; lon[2]=; lat[3]=38.2511608; lon[3]=-85.7236468;How do i handle this? Because if there is one error in my JS array, the google map wont show up at all. Is this something i should deal with in my JS code or my PHP code?this was my JS and PHP code:

<script type="text/javascript">	var lat = new Array(); var lon = new Array();<?php$request = 'some REST web servce';$returned_contents = file_get_contents($request);$myXML = simplexml_load_string($returned_contents); $i = 0;foreach( $myXML->events->event as $event) {	echo "lat[".$i."]=".$event->latitude."; ";	echo "lon[".$i."]=".$event->longitude."; ";	$i += 1; }?>	function initialize() {	  if (GBrowserIsCompatible()) {		var map = new GMap2(document.getElementById("map_canvas"));		map.setCenter(new GLatLng(37.4419, -122.1419), 1);		  		for(i=0; i<lat.length; i++) {		  var latlng = new GLatLng(lat[i],lon[i]);		  map.addOverlay(new GMarker(latlng));		}	  }}</script>

Link to comment
Share on other sites

So apparently the Google Maps API will plot variables if they are strings or numbers.lat[1] = -120.34343 would do the same as lat[1] = '-120.34343' not exactly sure why, but that solves the problem if there are holes if you are pulling coordinates from a web service or database. anyone know why? Thanks!

Link to comment
Share on other sites

I suspect that Google is validating the data, so it's converting everything to numbers and then making sure the conversions worked. A numeric string in Javascript will successfully convert to a number. Google has good programmers, so it would make sense that they are validating all of the data coming in before trying to use it. Try sending a non-numeric text string as one of the values and see if you get an error message from Google, but I'm sure they're validating everything and converting as needed.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...