PDA

View Full Version : Array Problem

DanHardy
01-27-2011, 05:24 PM
Hi all,

I am mixing googles geolocation with it's local search API.

This sounds complicated but I think my problem is primarily one involving passing a value to an array.

Here is my code

<script type="text/javascript">

var lat,lon;

var scrnText;

function main()
{

scrnText = document.getElementById('message');

if (navigator.geolocation) {

navigator.geolocation.getCurrentPosition(reportLatLon, function(error) {
scrnText.innerHTML = "Couldn't acquire location (error "+error.code+"). ";
});
}

else {
scrnText.innerHTML = "geolocation API not available!";
}
}

function reportLatLon(pos)
{

lat = pos.coords.latitude;
lon = pos.coords.longitude;
accuracy = Math.round(pos.coords.accuracy);

scrnText.innerHTML="your location is " + lat + ", " + lon + " to "+ accuracy + " metres accuracy";
}

</script>

<script type="text/javascript">

var localSearch;
function searchComplete() {

document.getElementById('content').innerHTML = '';
if (localSearch.results && localSearch.results.length > 0) {
for (var i = 0; i < localSearch.results.length; i++) {

var p = document.createElement('p');
var a = document.createElement('a');
var b = document.createElement('b');
var c = document.createElement('c');
a.href = localSearch.results[i].url;
a.innerHTML = localSearch.results[i].title;
b.innerHTML = "<br>" +
c.innerHTML = "<br>" +
localSearch.results[i].city + "," +
localSearch.results[i].region;

p.appendChild(a);
p.appendChild(b);
p.appendChild(c);
document.body.appendChild(p);
}
}
}

var lat,lng,str
navigator.geolocation.getCurrentPosition(handler);
function handler(position) {
lat = pos.coords.latitude;
lng = pos.coords.longitude;
str = lat + "," + lng;
localSearch.setCenterPoint(str);
}

localSearch.setSearchCompleteCallback(this, searchComplete, null);

localSearch.execute('cinema');

}

</script>

<!-- when this html page is loaded by a browser, it calls the 'main' javascript function -->

<h1>Geolocation Demo</h1>
<h2>1. GeoLocation API</h1>

<!-- this is where the html object updates text displayed to the user -->
<span id="message"></span>

<div id="branding" style="float: left;"></div><br />
<div id="content"></div>
</body>

All I want to do is pass the previous "lat,lon" value from the geolocation, into the "localSearch.setCentrepint part.

As you will see I have tried using the following

Code:

var lat,lng,str
navigator.geolocation.getCurrentPosition(handler);
function handler(position) {
lat = pos.coords.latitude;
lng = pos.coords.longitude;
str = lat + "," + lng;
localSearch.setCenterPoint(str);
}

but this will not work.

I am a php coder so let me apologise in advance if i have worded this confusingly.

Dan

venegal
01-27-2011, 06:02 PM

.setCenterPoint(location) targets search results to a specific location, where location supplies the location using one of three possible variants:

The center point of a map, using google.maps.Map2 in the presence of a getCenter() property.
A latitude/longitude combination, using google.maps.LatLng in the presence of an x property.
A string which specifies an address by name. When this argument is presented, searches are scoped as if the user performed a Google Local Search specifying near location as part of their search expression. Note: The string location is resolved into a google.maps.LatLng asynchronously so if this method is used, searches are made relative to this location once the geocoding operation completes with success.
If you do not specify this property, the default location is San Francisco, California.

So, the parameter has to be either one of those two objects or a string containing an address, and yours is none of that.

You'll want to use one of those objects, though, since you already got the coordinates.

DanHardy
01-27-2011, 07:12 PM
Could you care to venture an example of the code you are suggesting?

Like I say, I am a javascript novice so any help you can give would be greatly appreciated.

Thanks

Dan

venegal
01-27-2011, 08:04 PM
You'd first have to load the maps API:

You probably have to make sure that it's available before doing stuff with it, so better put the rest in a callback: