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) {

scrnText.innerHTML = "acquiring your position...";

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 src="http://www.google.com/jsapi?key=AIzaSyA5m1Nc8ws2BbmPRwKu5gFradvD_hgq6G0" type="text/javascript"></script>
<script type="text/javascript">

google.load('search', '1');

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>" +
localSearch.results[i].streetAddress;
c.innerHTML = "<br>" +
localSearch.results[i].city + "," +
localSearch.results[i].region;


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

function onLoad() {


localSearch = new google.search.LocalSearch();


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');


google.search.Search.getBranding('branding');
}


google.setOnLoadCallback(onLoad);
</script>

</head>

<!-- when this html page is loaded by a browser, it calls the 'main' javascript function -->
<body onload="main()">

<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.


Thanks in advance

Dan

venegal
01-27-2011, 06:02 PM
See the API reference (http://code.google.com/intl/de-DE/apis/maps/documentation/localsearch/reference.html):


.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.

The preferred interface is either google.maps.LatLng() or google.maps.Map2().

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:

google.load("maps", "2");

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

google.load("maps", "2", "callback": yourCallbackFunction);

There, you can then create your latlng object:

var latlng = new google.maps.LatLng(yourLat, yourLng);

And use this as parameter for setCenterPoint:

localSearch.setCenterPoint(latlng);

Generally, reading the API documentation will help you, novice or not.