watchPosition - getCurrentPosition conflict
Dit werkt heel raar. Als ik op de knop klik gebeurd er niks, maar nadat ik 10 keer op de knop heb geklikt en ik in CHROME een ander venster open en weer terug ga, dan laad hij hem 10 keer.
hier is de getCurLoc function
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
function getCurLoc(location)
{
if (navigator.geolocation)
{
// bij chrome werkt dit alleen op https
navigator.geolocation.getCurrentPosition(function(position)
{
var geocoder;geocoder=new google.maps.Geocoder;var latlng=new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
//alert("Else loop" + latlng);
lat = position.coords.latitude;
lng = position.coords.longitude;
geocoder.geocode({'latLng': latlng}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
if (results[0])
{
// je kunt alleen jezelf op de map zien, wanneer je in ID bent, anders kan je nog wel kijken, maar verder niets doen.
//var country=results[0]['address_components'][6].short_name;
var country =getCountry(results[0].address_components)
// extract country short name (e.g. GB for Great Britain) from google geocode API result
var add = results[0].formatted_address;
//alert(add);
var street=results[0]['address_components'][1].long_name;
var nr=results[0]['address_components'][0].long_name;
if (location == 'curreg') // wanneer men registreerd en hun locatie wil weten
{
//$(".loading-div").show();
$.ajax({
type: "POST",
url: "https://www.mijndomein.com/inc/current_reg_location.php",
data: {
"lat": position.coords.latitude,
"lng": position.coords.longitude,
"street": street,
"nr": nr
},
dataType: "json",
encode: !0
}).done(function(e) {
//$(".loading-div").hide();
$(".regions.district.adcatselect").empty(), $(".regions.city.adcatselect.valid").empty(),
//$(".regions.province.adcatselect.valid").empty(),
$("#iso").val(e.iso);
$("<option></option>", {
value: e.reg1,
text: e.reg1,
selected: "selected"
}).appendTo(".regions.province"),
$("<option></option>", {
value: e.reg2,
text: e.reg2,
selected: "selected"
}).appendTo(".regions.city")
// alert(e.reg3);
if (e.reg3 == '' || e.reg3 == undefined)
{
$("<option></option>", {value: '', text: '<?=$_SESSION['def']['NO_DISTRICT']?>', selected: 'selected'}).appendTo('.regions.district');
} else {
$("<option></option>", {value: e.reg3, text: e.reg3, selected: 'selected'}).appendTo('.regions.district');
}
$("#lng").val(position.coords.longitude),$("#lat").val(position.coords.latitude);
})
} else { //alert(city);
if (street !== '')
{ // SESSIE AANMAKEN in current_add.php
//alert(position.coords.longitude);
//$(".loading-div").show()
$.ajax({
type: "POST",
url: "https://www.mijndomein.com/inc/current_add.php",
data: {
lat: position.coords.latitude,
lng: position.coords.longitude,
street: street,
nr: nr
},
dataType: "html",
encode: !0
}).done(function(t) {
$(".loading-div").hide();
$(".opradius,.opradiuss").attr("cur", "1"), $(".loading-div").hide(), $(".current").html(t);
initMap(lat,lng);
});
} else {
//$(".loading-div").hide();
$('.current').html('<li><?=$_SESSION['def']['LOC_NOT_FOUND']?></li>');
}
}
}
}
})
},error)// end navigator
}
}
{
if (navigator.geolocation)
{
// bij chrome werkt dit alleen op https
navigator.geolocation.getCurrentPosition(function(position)
{
var geocoder;geocoder=new google.maps.Geocoder;var latlng=new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
//alert("Else loop" + latlng);
lat = position.coords.latitude;
lng = position.coords.longitude;
geocoder.geocode({'latLng': latlng}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
if (results[0])
{
// je kunt alleen jezelf op de map zien, wanneer je in ID bent, anders kan je nog wel kijken, maar verder niets doen.
//var country=results[0]['address_components'][6].short_name;
var country =getCountry(results[0].address_components)
// extract country short name (e.g. GB for Great Britain) from google geocode API result
var add = results[0].formatted_address;
//alert(add);
var street=results[0]['address_components'][1].long_name;
var nr=results[0]['address_components'][0].long_name;
if (location == 'curreg') // wanneer men registreerd en hun locatie wil weten
{
//$(".loading-div").show();
$.ajax({
type: "POST",
url: "https://www.mijndomein.com/inc/current_reg_location.php",
data: {
"lat": position.coords.latitude,
"lng": position.coords.longitude,
"street": street,
"nr": nr
},
dataType: "json",
encode: !0
}).done(function(e) {
//$(".loading-div").hide();
$(".regions.district.adcatselect").empty(), $(".regions.city.adcatselect.valid").empty(),
//$(".regions.province.adcatselect.valid").empty(),
$("#iso").val(e.iso);
$("<option></option>", {
value: e.reg1,
text: e.reg1,
selected: "selected"
}).appendTo(".regions.province"),
$("<option></option>", {
value: e.reg2,
text: e.reg2,
selected: "selected"
}).appendTo(".regions.city")
// alert(e.reg3);
if (e.reg3 == '' || e.reg3 == undefined)
{
$("<option></option>", {value: '', text: '<?=$_SESSION['def']['NO_DISTRICT']?>', selected: 'selected'}).appendTo('.regions.district');
} else {
$("<option></option>", {value: e.reg3, text: e.reg3, selected: 'selected'}).appendTo('.regions.district');
}
$("#lng").val(position.coords.longitude),$("#lat").val(position.coords.latitude);
})
} else { //alert(city);
if (street !== '')
{ // SESSIE AANMAKEN in current_add.php
//alert(position.coords.longitude);
//$(".loading-div").show()
$.ajax({
type: "POST",
url: "https://www.mijndomein.com/inc/current_add.php",
data: {
lat: position.coords.latitude,
lng: position.coords.longitude,
street: street,
nr: nr
},
dataType: "html",
encode: !0
}).done(function(t) {
$(".loading-div").hide();
$(".opradius,.opradiuss").attr("cur", "1"), $(".loading-div").hide(), $(".current").html(t);
initMap(lat,lng);
});
} else {
//$(".loading-div").hide();
$('.current').html('<li><?=$_SESSION['def']['LOC_NOT_FOUND']?></li>');
}
}
}
}
})
},error)// end navigator
}
}
Daarnaast heb ik ook deze functie met watchID wat live alles bijhoud. Dit conflicteerd met elkaar.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var options = {enableHighAccuracy: false,timeout: 5000,maximumAge: 0,desiredAccuracy: 0, frequency: 1 };
var oldlat = 0;
var oldlng = 0;
watchID = navigator.geolocation.watchPosition(callback, error, options);
function callback(position) {
var lat = position.coords.latitude.toFixed(2);
var lng = position.coords.longitude.toFixed(2);
var gm = getmeters(oldlat,oldlng,lat,lng);
// alert(gm);
if ( (gm > 10) && (oldlat!= 0 && oldlng != 0) ) {
return;
}
oldlat = lat;
oldlng = lng;
$.ajax({
type: "GET",
url: "<?=HTML_ROOT?>inc/addcurrentloc.php",
data: {
lat: lat,lng:lng
},
success: function(t) {
}
});
}
var oldlat = 0;
var oldlng = 0;
watchID = navigator.geolocation.watchPosition(callback, error, options);
function callback(position) {
var lat = position.coords.latitude.toFixed(2);
var lng = position.coords.longitude.toFixed(2);
var gm = getmeters(oldlat,oldlng,lat,lng);
// alert(gm);
if ( (gm > 10) && (oldlat!= 0 && oldlng != 0) ) {
return;
}
oldlat = lat;
oldlng = lng;
$.ajax({
type: "GET",
url: "<?=HTML_ROOT?>inc/addcurrentloc.php",
data: {
lat: lat,lng:lng
},
success: function(t) {
}
});
}
Ik wil dus gewoon als men op de knop klikt dat getcurloc word uitgevoerd en niet alleen als ik naar een ander venster ga en weer terug kom:
Toevoeging op 20/04/2017 18:10:34:
Heb het al gevonden, het probleem was dat ik watchID eerst moet stoppen met navigator.geolocation.clearWatch(watchID);
Daarna kan je navigator.geolocation.getCurrentPosition aanroepen zonder raar gedrag.
Er zijn nog geen reacties op dit bericht.