简体   繁体   English

Javascript 查找从谷歌地图获取纬度和经度

[英]Javascript to find get latitude and longitude from Google Maps

I'm trying to pull the latitude/longitude from an API call to Google Maps.我试图从 API 调用中提取纬度/经度到谷歌地图。 The code I am using is the following:我正在使用的代码如下:

var text gMaps = "https://maps.googleapis.com/maps/api/geocode/xml?address=" & URLEncode(SearchAndReplace([Related Staff - Address]," ","+")) & "&key=xxxxxxxxxxxxxxxxxxxx";

var text lat = URLRoot() & "db/" & Dbid() & "?act=API_EditRecord" & "&rid=" & [Record ID#] & "&apptoken=xxxxxxxxxxxxx" & "&_fid_113=";    

    "javascript: {" &
   
         "$.get('" & $gMaps & "',function(data,success) {" &
            "console.log(data);" &
            "var geo = data.getElementsByTagName('geometry')[0].getAttribute('lat');"  &
            "console.log(geo);" & 
            "$.get('" & $lat & "'+geo);" &
            "window.location.reload();" &
        "});" &
"}"

This is for a button in Quickbase to put the info into 2 fields.这是 Quickbase 中用于将信息放入 2 个字段的按钮。 The example above is only dealing with the latitude for now.上面的例子现在只处理纬度。 This code is giving me a null result, I'm familiar with javascript, but still need a lot of learning.这段代码给了我一个 null 结果,我熟悉 javascript,但仍然需要大量学习。

Here's the xml response that I'm pulling from.这是我从中提取的 xml 响应。

XML

Thanks for any help provided.感谢您提供的任何帮助。

Dana达纳

You can use Javascript's DOMParser and XPath features to extract the lat and lng from an XML string:您可以使用 Javascript 的DOMParserXPath功能从 XML 字符串中提取latlng

let xmlString = `
    <GeocodeResponse>
     <status>OK</status>
     <result>
      <type>sample</type>
      <name>Sample XML</name>
      <location>
       <lat>37.4217550</lat>
       <lng>-122.0846330</lng>
      </location>
     </result>
     <result>
      <message>The secret message</message>
     </result>
    </GeocodeResponse>
`

var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xmlString, "text/xml"); //important to use "text/xml"

var nsResolver = xmlDoc.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);

var lat = xmlDoc.evaluate('//lat', xmlDoc, nsResolver, XPathResult.STRING_TYPE, null ).stringValue;
var lng = xmlDoc.evaluate('//lng', xmlDoc, nsResolver, XPathResult.STRING_TYPE, null ).stringValue;

console.log(lat, lng) // 37.4217550 -122.0846330

Also see Parse XML file with JavaScript and plot on Google Maps另请参阅在 Google 地图上使用 JavaScript 和 plot 解析 XML 文件

It doesn't look like you're finishing your lat query string variable.看起来您并没有完成您的 lat 查询字符串变量。 Your variable ends up being "yourdomain.quickbase.com/db/yourdbid?act=API_EditRecord&apptoken=&rid=&_fid_113=" and so fid 113 is never actually assigned a value.您的变量最终是 "yourdomain.quickbase.com/db/yourdbid?act=API_EditRecord&apptoken=&rid=&_fid_113=" 因此 fid 113 从未真正分配过值。 Even though you're running the JavaScript below no where is it being told to be assigned to fid 113. Also, it's worth nothing that JavaScript injection is soon going to be a thing of the past in Quick Base so this might be something you want to throw into a code page instead which would be easier anyways because then you could use the geocoder object that the Google Maps API provides.即使您在下面运行 JavaScript,也没有人告诉它分配给 fid 113。此外,JavaScript 注入很快将在 Quick Base 中成为过去,因此这可能是您想要的。改为放入代码页,这无论如何都会更容易,因为这样您就可以使用谷歌地图 API 提供的地理编码器 object。

I would advise to use a Code Page inside of Quick Base instead a formula field for this solution.我建议在 Quick Base 中使用代码页而不是此解决方案的公式字段。 With a code page you can brig in an XML parser library to handle the response and then make then next API call to Quick Base with the latitude and longitude values.使用代码页,您可以使用 XML 解析器库来处理响应,然后使用纬度和经度值对 Quick Base 进行下一个 API 调用。 That would be the best practice.那将是最佳做法。

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

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