简体   繁体   English

使用Google Maps API从本地存储向Google Maps加载坐标

[英]Load coordinates to Google Maps from localstorage with Google Maps API

When a user clicks a button, it will keep refreshing their location and pushing it to an array and in the end when the user clicks another button,it should show their path from the time the start button was clicked: 当用户单击按钮时,它将继续刷新其位置并将其推入数组,最后,当用户单击另一个按钮时,它应显示从单击开始按钮起的路径:

    var refreshIntervalId = setInterval(recordtrip, 5000);

function recordtrip(){
if (navigator.geolocation) {        
    navigator.geolocation.getCurrentPosition(function(position){
    x = position;

    triplog.push(new google.maps.LatLng(x.coords.latitude,x.coords.longitude));

$('#stoptrip').click(function(){
clearInterval(refreshIntervalId);

});
});
$('#showtrip').click(function(){
var lenoftrip = triplog.length - 1 ;

var mapProp = {
  center:triplog[lenoftrip],
  zoom:20,
  mapTypeId:google.maps.MapTypeId.ROADMAP
  };

var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

var marker=new google.maps.Marker({
  position:triplog[lenoftrip],
  });

marker.setMap(map);
var flightPath=new google.maps.Polyline({
  path:triplog,
  strokeColor:"#0000FF",
  strokeOpacity:0.8,
  strokeWeight:2
  });

flightPath.setMap(map);

});

It works well before saving to localstorage, but after saving and loading it,it doesn't work anymore. 在保存到本地存储之前,它运行良好,但是在保存和加载后,它不再起作用。 I realized then when i call the array from localstorage,it removes the new google.maps.LatLng . 我意识到,当我从localstorage调用数组时,它将删除new google.maps.LatLng This is the saving function: 这是保存功能:

$('#savetrip').click(function(){
localStorage.setItem(titleoftrip,JSON.stringify(triplog));
alert("Saved!");
triplog = [];
}

How do you fix this? 您如何解决这个问题?

Sorry if the answer is a bit confusing but I solved the problem I was having: I realised that the problem was that it was giving errors while parsing the data.I think the reason for this was that I was saving the array triplog in which each piece of data stored was new google.maps.LatLng(x.coords.latitude,x.coords.longitude) . 很抱歉,答案有点混乱,但我解决了我遇到的问题:我意识到问题在于解析数据时出现错误,我认为原因是我保存了数组triplog ,每个存储的数据是new google.maps.LatLng(x.coords.latitude,x.coords.longitude) So i created a new array triplogsave which stored only x.coords.latitude,x.coords.longitude and saved that to localStorage and when I called that and parsed it, it worked without any errors. 因此,我创建了一个新的数组triplogsave ,该数组triplogsave存储x.coords.latitude,x.coords.longitude并将其保存到localStorage,当我调用并解析它时,它可以正常工作而没有任何错误。 the edited code is: 编辑后的代码为:

var triplog =[];
var triplogsave = [];
var refreshIntervalId = setInterval(recordtrip, 5000);

function recordtrip(){
if (navigator.geolocation) {        
    navigator.geolocation.getCurrentPosition(function(position){
    x = position;

    triplog.push(new google.maps.LatLng(x.coords.latitude,x.coords.longitude));
triplogsave.push(x.coords.latitude,x.coords.longitude);

$('#stoptrip').click(function(){
clearInterval(refreshIntervalId);

});
});
$('#showtrip').click(function(){
var lenoftrip = triplog.length - 1 ;

var mapProp = {
  center:triplog[lenoftrip],
  zoom:20,
  mapTypeId:google.maps.MapTypeId.ROADMAP
  };

var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

var marker=new google.maps.Marker({
  position:triplog[lenoftrip],
  });

marker.setMap(map);
var flightPath=new google.maps.Polyline({
  path:triplog,
  strokeColor:"#0000FF",
  strokeOpacity:0.8,
  strokeWeight:2
  });

flightPath.setMap(map);

});

$('#savetrip').click(function(){
localStorage.setItem(titleoftrip,JSON.stringify(triplog));
alert("Saved!");
triplog = [];
}

so after saving and loading,it loads triologsave instead of triplog,parses it and loads that array into a google map. 因此,在保存和加载后,它将加载triologsave而不是triplog,对其进行解析并将该数组加载到google map中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM