I am little new in JavaScript and Google Map. I am trying to draw lines between my multiple location. I am following this example.
My Code is like this
JavaScript
x
97
97
1
<script type="text/javascript">
2
function pinSymbol(color) {
3
return {
4
path: 'M 0,0 C -2,-20 -10,-22 -10,-30 A 10,10 0 1,1 10,-30 C 10,-22 2,-20 0,0 z',
5
fillColor: color,
6
fillOpacity: 1,
7
strokeColor: '#000',
8
strokeWeight: 2,
9
scale: 2
10
};
11
}
12
13
14
$.ajax({
15
type:'get',
16
url:'https://example.com/info.php',
17
data:{
18
19
},
20
dataType:'json',
21
success:function(result){
22
var UserLatitude;
23
var UserLongitude;
24
var color = "red";
25
var last ;
26
27
var MyData = result['Result'];
28
var InformationArray = [];
29
var LocationArray = [];
30
31
for(var i=0; i<MyData.length; i++){
32
var UserLatitude = parseFloat(MyData[i]['UserLatitude']);
33
var UserLongitude = parseFloat(MyData[i]['UserLongitude']);
34
var LastTrackedOn = MyData[i]['LastTrackedOn'];
35
var InSchoolLocation = MyData[i]['InSchoolLocation'];
36
37
InformationArray.push([ LastTrackedOn, UserLatitude, UserLongitude, InSchoolLocation ]);
38
LocationArray.push([UserLatitude,UserLongitude]);
39
40
}
41
42
console.log(InformationArray);
43
console.log(LocationArray);
44
45
var map = new google.maps.Map(document.getElementById('map'), {
46
zoom: 12,
47
center: new google.maps.LatLng(21.947275, 71.720822222)
48
49
});
50
51
var infowindow = new google.maps.InfoWindow();
52
53
var marker, i;
54
55
for (i = 0; i < InformationArray.length; i++) {
56
var color = "red";
57
if(InformationArray[i][3] == true){
58
color= "green";
59
}
60
61
marker = new google.maps.Marker({
62
position: new google.maps.LatLng(LocationArray[i][0], LocationArray[i][1]),
63
map: map,
64
icon: pinSymbol(color)
65
});
66
67
google.maps.event.addListener(marker, 'click', (function(marker, i) {
68
return function() {
69
infowindow.setContent("Tracked On: "+InformationArray[i][0]+ " ");
70
infowindow.open(map, marker);
71
}
72
})(marker, i));
73
}
74
75
76
var line= new google.maps.Polyline({
77
path: LocationArray,
78
geodesic: true,
79
strokeColor: '#FF0000',
80
strokeOpacity: 1.0,
81
strokeWeight: 2
82
});
83
84
line.setMap(map);
85
86
87
88
89
90
91
}
92
93
});
94
95
96
</script>
97
Its showing places fine and without issue. Its just not drawing the lines. I am getting error like below
JavaScript
1
2
1
InvalidValueError: at index 0: not a LatLng or LatLngLiteral with finite coordinates: in property lat: not a number
2
I am not getting idea what I should do for fix the issue. Let me know if any expert here can help me for the same. Thanks!
Advertisement
Answer
You should build a proper LatLng Obj
JavaScript
1
2
1
LocationArray.push(new google.maps.LatLng(UserLatitude, UserLongitude));
2