如何将Google LatLng对象串联起来? [英] How do I stringify a google LatLng object?

查看:132
本文介绍了如何将Google LatLng对象串联起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google地图API并尝试对各个点都有经纬度的点进行串联。我想要做的是将数据串联起来并将其存储在数据库中,以便稍后可以检索它。问题是,由于谷歌地图存储和使用数据的方式, JSON.stringify 的结果有时是奇怪的随机数。例如,有时数据如下所示:

  [{Na:35.99663,Oa: -  78.94982}, 
{Na:35.996370000000006,Oa: - 78.94914},
{Na:35.99595,Oa: - 78.94833000000001},...]

$ b有时完全相同的数据如下所示:

  [{Ia:35.99663,Ja: -  78.94982},
{Ia:35.996370000000006,Ja: - 78.94914},
{Ia:35.99595 ,Ja: - 78.94833000000001},...]

不同的变化。当我将字符串化的数据存储到数据库中并检索它时,这会导致很多问题,因为我永远不知道要使用哪种格式。我想要做的是替换键,使数据看起来更像这样:

  [{lat:35.99663, lon: -  78.94982},
{lat:35.996370000000006,lon: - 78.94914},
{lat:35.99595,lon: - 78.94833000000001},...]

我已经编写代码来确定Google地图正在使用哪些随机字母。有了这些信息,我想用lat或lon替换每个出现的随机字母。我希望使用像这样的替代方法...

 函数formatData(data){
// data is一个对象,我想stringify

//这个testPoint和testString帮助我们找出哪些谷歌正在使用这一次使用
var testPoint = new google.maps.LatLng(23,45 );
var testString = JSON.stringify(testPoint);
var strangeLat = testString.substring(2,4); //这是一个随机的两个字符串,表示字符串数据中的纬度
var strangeLon = testString.substring(10,12); //这是一个随机的两个字符串,表示字符串数据中的经度

//将数据字符串化,并用lat和lon替换所有随机字母的出现
var formattedData = JSON。 stringify(数据,函数(键,值){
//在这里执行某些操作来替换键
});

返回formattedData;
}

我知道如何用替代函数替换值,但是我不能找出如何更换钥匙。我搜索了一下,发现了一些类似的案例: JSON.stringify,更改关键的情况下替换JavaScript中的JSON键。但是,我很难理解它们,因此无法提出解决方案来满足我的需求。



感谢您提供任何帮助或建议!




更新!



谢谢大家。我能够结合您的建议想出一个解决方案。下面是它的样子:

 函数formatData(data){
// data是一个包含许多LatLng对象的数组

newData = [];

for(var i = 0; i< data.length; i ++){
var obj = new Object();
obj ['lat'] = data [i] .lat();
obj ['lon'] = data [i] .lng();
newData.push(obj);
}

返回JSON.stringify(newData);


解决方案


$ b $

  var ar = [{Ia:35.99663,Ja: -  78.94982},{Ia:35.996370000000006 , JA: -  78.94914},{ IA:35.99595, JA: -  78.94833000000001}]; 

var newAr = [];
for(var i = 0; i< ar.length; i ++)
{
var obj = new Object();
var u = 0;
for(var x in ar [i])
{

if(u == 0)
obj ['lat'] = ar [i] [ X];
else
obj ['lon'] = ar [i] [x];
u ++;
}
newAr.push(obj);
}
alert(newAr.toSource());

我做了什么:
1.我创建了新的数组
2 。然后我循环遍历ar
3.然后我循环遍历ar
中的对象属性,并且我已经将属性值赋给了新属性



那就是

I'm working with the google maps API and trying to stringify an array of points, which each have a latitude and longitude. What I want to do is stringify the data and store it in a database so that it can be retrieved later. The problem is that, because of the way google maps stores and uses data, the result of JSON.stringify is strangely random at times. For example, sometimes the data looks like this:

[{"Na":35.99663,"Oa":-78.94982},
 {"Na":35.996370000000006,"Oa":-78.94914},
 {"Na":35.99595,"Oa":-78.94833000000001},...]

And sometimes the exact same data looks like this:

[{"Ia":35.99663,"Ja":-78.94982},
 {"Ia":35.996370000000006,"Ja":-78.94914},
 {"Ia":35.99595,"Ja":-78.94833000000001},...]

In fact I've seen about ten different variations. This causes a lot of problems when I store the stringified data in the database and retrieve it, because I never know which format to expect. What I want to do is replace the keys so that the data looks more like this:

[{"lat":35.99663,"lon":-78.94982},
 {"lat":35.996370000000006,"lon":-78.94914},
 {"lat":35.99595,"lon":-78.94833000000001},...]

I've already written code to figure out which random letters google maps is using. With that information, I want to replace each occurrence of those random letters with "lat" or "lon". I hope to use a replacer method like so...

function formatData(data) {
    //data is an object that I want to stringify

    //this testPoint and testString help us figure out which letters google is using this time around
    var testPoint = new google.maps.LatLng(23,45);
    var testString = JSON.stringify(testPoint);
    var strangeLat = testString.substring(2,4); //this is a random two character string which represents latitude in the stringified data
    var strangeLon = testString.substring(10,12); //this is a random two character string which represents longitude in the stringified data

    //stringify the data and replace all occurences of the random letters with lat and lon
    var formattedData = JSON.stringify(data, function(key,value) {
        //do something here to replace the keys
    });

    return formattedData;     
}

I know how to replace values with the replacer function, but I can't figure out how to replace keys. I searched around and found a few similar cases online: JSON.stringify, change the case of the key and Replace keys JSON in javascript . However, I had trouble understanding them and was unable to come up with a solution to fit my needs.

Thanks in advance for any help or suggestions!


Update!

Thanks all. I was able to come up with a solution using a combination of your suggestions. Here's what it looks like:

function formatData(data) {
    //data is an array of many LatLng objects

    newData = [];

    for(var i = 0; i < data.length; i++) {
        var obj = new Object();
        obj['lat'] = data[i].lat();
        obj['lon'] = data[i].lng();
        newData.push(obj);
    }

    return JSON.stringify(newData);     
}

解决方案

Method below will do what you want

var ar = [{"Ia":35.99663,"Ja":-78.94982},{"Ia":35.996370000000006,"Ja":-78.94914},{"Ia":35.99595,"Ja":-78.94833000000001}];

var newAr = [];
for(var i = 0; i < ar.length; i++)
{
   var obj = new Object();
   var u = 0;
   for(var x in ar[i])
   { 

        if(u == 0)
         obj['lat'] = ar[i][x];
        else
         obj['lon'] = ar[i][x];
      u++;
    }
    newAr.push(obj);
}
alert(newAr.toSource());

What I have done: 1. I have created new array 2. Then I have looped through ar 3. Then I have looped through object properties from ar 4. and I have assigned values of properties to new properties

and that is it

这篇关于如何将Google LatLng对象串联起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆