I'm using phonegape, google maps api v3 and navigator.geolocation.watchPosition function in my project.
I try to follow my position. I have problem with markers. Application working good, but doesn't remove old markers. It creates new markers all time, when i will get new coords.
function onSuccess(position) {
var image = 'arrow1.png';
var pos_table = ['Here I am', position.coords.latitude, position.coords.longitude];
var where_i_am = new google.maps.LatLng(pos_table[1], pos_table[2]);
var my_position = new google.maps.Marker({
position: where_i_am,
map: map,
title: pos_table[0],
icon: image
});
map.panTo(new google.maps.LatLng(
position.coords.latitude,
position.coords.longitude
));
}
I added setmap(null), but now there aren't any markers on the map
for (var i = 0; i < my_position.length; i++) {
my_position[i].setMap(null);
}
When I try :
if (my_position) {
for (var i = 0; i < my_position.length; i++) {
my_position[i].setMap(null);
}
}
it dosen't work at all. Please help me. Thx.
Instead of trying to delete old markers, you could try to use update the position of the old marker.
var my_position;
function onSuccess(position) {
var image = 'arrow1.png';
var pos_table = ['Here I am', position.coords.latitude, position.coords.longitude];
var where_i_am = new google.maps.LatLng(pos_table[1], pos_table[2]);
if (my_position) {
my_position.setPosition(where_i_am);
} else {
my_position = new google.maps.Marker({
position: where_i_am,
map: map,
title: pos_table[0],
icon: image
});
}
map.panTo(where_i_am);
}
Change to
var my_position = new Array();
function onSuccess(position) {
for(var i = 0; i< 10; i++){
var image = 'arrow1.png';
var pos_table = ['Here I am', position.coords.latitude, position.coords.longitude];
var where_i_am = new google.maps.LatLng(pos_table[1], pos_table[2]);
my_position[i] = new google.maps.Marker({
position: where_i_am,
map: map,
title: pos_table[0],
icon: image
});
map.panTo(new google.maps.LatLng(
position.coords.latitude,
position.coords.longitude
));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.