Hi
I'm trying to create a map for iphone which re-centres everytime the iphone
position updates. I have most of it working (see below) i.e. retrieving
position, showing map etc. but I'm stuck on starting the map centred at the
position retrieved and then moving it as it updates. Pretty sure it's
something simple, everything seems to be there but perhaps map.panTo and
map.setCenter are V2?
Any help or suggestions much appreciated. Thanks in advance.
Garrett
var start_stop_btn, wpid=false, map, z, op, prev_lat, prev_long,
min_speed=0, max_speed=0, min_altitude=0, max_altitude=0,
distance_travelled=0, min_accuracy=150, date_pos_updated="", info_string="";
// This function just adds a leading "0" to time/date components which are
<10 (because there is no cross-browser way I know of to do this using the
date object)
function format_time_component(time_component)
{
if (time_component < 10)
{
time_component = "0" + time_component;
}
else if (time_component.length<2)
{
time_component = time_component + "0";
}
return time_component;
}
// This is the function which is called each time the Geo location position
is updated
function geo_success(position)
{
start_stop_btn.innerHTML = "Stop"; // Set the label on the start/stop
button to "Stop"
info_string = "";
var d = new Date(); // Date object, used below for output messahe
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
var current_datetime = format_time_component(h) + ":" +
format_time_component(m) + ":" + format_time_component(s);
// Check that the accuracy of our Geo location is sufficient for our
needs
if(position.coords.accuracy <= min_accuracy)
{
// We don't want to action anything if our position hasn't changed -
we need this because on IPhone Safari at least, we get repeated readings of
the same location with
// different accuracy which seems to count as a different reading -
maybe it's just a very slightly different reading or maybe altitude,
accuracy etc has changed
if (prev_lat != position.coords.latitude || prev_long !=
position.coords.longitude)
{
//check if a speed has been returned (iphones without compass)
if (position.coords.speed != 0)
{
//record max and min speed
if (position.coords.speed > max_speed)
max_speed=position.coords.speed;
if(position.coords.speed < min_speed) min_speed =
position.coords.speed;
}
//check if altitude has been returned (iphones without compass)
if (position.coords.altitude != 0)
{
//record max and min altitude
if (position.coords.altitude > max_altitude) max_altitude =
position.coords.altitude;
if (position.coords.altitude < min_altitude) min_altitude =
position.coords.altitude;
}
//store latitude and longitude for next check
prev_lat = position.coords.latitude;
prev_long = position.coords.longitude;
info_string = "<span class=\"title\" id=\"title\">Current
positon</span><br />Latitude: " + position.coords.latitude + "<br
/>Longitude: " + position.coords.longitude + "<br />(accuracy
"+Math.round(position.coords.accuracy, 1) + "m)<br /><br />Last reading
taken at: " + current_datetime;
//for iphone 4
//Speed: min = " + (min_speed?min_speed:"Not recorded/0") +
"m/s, max = " + (max_speed?max_speed:"Not recorded/0") + "m/s<br />Altitude:
min = " + (min_altitude?min_altitude:"Not recorded/0") + "m, max = " +
(max_altitude?max_altitude:"Not recorded/0") + "m (accuracy " +
Math.round(position.coords.altitudeAccuracy,1) + "m)";
}
}
else
{
info_string = "Accuracy not sufficient (" +
Math.round(position.coords.accuracy, 1) + "m vs " + min_accuracy+"m)<br
/>Last reading taken at: " + current_datetime;
}
if (info_string)
{
op.innerHTML = info_string;
updatemap();
}
}
//issue here: when the gps position up dates I want to recentre the map
there
//I've tried map.setCenter(new GLatLng(prev_lat, prev_long), 13); and panTo
//below but nether seem to do anything
function updatemap()
{
map.panTo(new GLatLng(prev_lat, prev_long));
}
// This function is called each time navigator.geolocation.watchPosition()
generates an error (i.e. cannot get a Geo location reading)
function geo_error(error)
{
switch(error.code)
{
case error.TIMEOUT:
op.innerHTML = "Timeout!";
break;
};
}
function get_pos()
{
// Set up a watchPosition to constantly monitor the geo location
provided by the browser - NOTE: !! forces a boolean response
// We use watchPosition rather than getPosition since it more easily
allows (on IPhone at least) the browser/device to refine the geo location
rather than simply taking the first available position
// Full spec for navigator.geolocation can be found here:
http://dev.w3.org/geo/api/spec-source.html#geolocation_interface
// First, check that the Browser is capable
if(!!navigator.geolocation)
{
wpid = navigator.geolocation.watchPosition(geo_success, geo_error,
{enableHighAccuracy:true, maximumAge:30000, timeout:27000});
}
else
{
op.innerHTML = "ERROR: Your Browser doesnt support the Geo Location
API";
}
}
// Initialiser: This will find the output message div and set up the actions
on the start/stop button
function init_geo()
{
op = document.getElementById("output"); // Note the op is defined in
global space and is therefore globally available
if (op)
{
start_stop_btn = document.getElementById("geo_start_stop");
if (start_stop_btn)
{
start_stop_btn.onclick = function()
{
if (wpid) // If we already have a wpid which is the ID
returned by navigator.geolocation.watchPosition()
{
start_stop_btn.innerHTML = "Start";
navigator.geolocation.clearWatch(wpid);
wpid = false;
}
else // Else...We should only ever get here right at the
start of the process
{
start_stop_btn.innerHTML = "Aquiring Geo Location...";
get_pos();
//issue here: how do I get the initial latitude and longitude to centre the
map
//I have tried navigator.geolocation.getCurrentPosition and then pulling out
the
//coords to insert in the next line but it does not seem to work?
var latlng = new google.maps.LatLng(31.4419,
-129.1419);
var myOptions = {
zoom: 8,
disableDefaultUI: true,
center: latlng,
mapTypeId: google.maps.MapTypeId.SATELLITE
};
//generate map
var map = new
google.maps.Map(document.getElementById("google_map"), myOptions);
}
}
}
else
{
op.innerHTML = "ERROR: Couldn't find the start/stop button";
}
}
}
// Initialise the whole system (above)
window.onload = init_geo;
--
You received this message because you are subscribed to the Google Groups
"Google Maps JavaScript API v3" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-maps-js-api-v3?hl=en.