Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从地址获取纬度和经度并写入文件_Javascript_Php_Html_Google Maps_File Io - Fatal编程技术网

Javascript 从地址获取纬度和经度并写入文件

Javascript 从地址获取纬度和经度并写入文件,javascript,php,html,google-maps,file-io,Javascript,Php,Html,Google Maps,File Io,这件我有点麻烦。我觉得使用javascript是不可能做到这一点的 我有一个文本文件,格式如下: Street # STREET CITY ST ZIP 全部用/t分隔,我想把这个地址转换成纬度和经度坐标 因此,我们的想法是从文本文件中读取一行,然后写入到同一个本地文件中,或者写入到一个全新的文件中(如果这更容易的话)我只需要做一次,只需一次将地址转换为纬度和经度。将不在应用程序中,只需要为我自己使用一个纬度和经度列表(大约需要转换200个地址) 我不确定在不使用Googl

这件我有点麻烦。我觉得使用javascript是不可能做到这一点的

我有一个文本文件,格式如下:

Street #    STREET  CITY    ST  ZIP  
全部用/t分隔,我想把这个地址转换成纬度和经度坐标

因此,我们的想法是从文本文件中读取一行,然后写入到同一个本地文件中,或者写入到一个全新的文件中(如果这更容易的话)我只需要做一次,只需一次将地址转换为纬度和经度。将不在应用程序中,只需要为我自己使用一个纬度和经度列表(大约需要转换200个地址)

我不确定在不使用Google Maps API的情况下是否有办法做到这一点,但我知道我可以通过以下方式检索纬度和经度:

        geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              var latlong = results[0].geometry.location;
            } else {
              alert('Geocode was not successful for the following reason: ' + status);
            }
       });
因此,我不仅不知道是否有可能在浏览器中使用js写入文件,而且我还知道上面的函数是异步运行的,可能会把它弄得一团糟

有什么想法吗?如果这在另一种语言中是可行的,那会更容易吗


如果您使用的是JQuery,那就谢谢了

geocoder.geocode( { 'address': address }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        var latlong = results[0].geometry.location;

        $.post('latlong.php', {'latitude': latlong.latitude, 'longitude': latlong.longitude}, function(res) {
            if (!res.success)
            {
                alert('Something went wrong: '+res.error);
            }
        }, 'JSON');

    } else {
        alert('Geocode was not successful for the following reason: ' + status);
    }
});
现在,latlong.php

if (isset($_POST['latitude']) && isset($_POST['longitude']))
{
    $coordinate = (object) $_POST;
    $file = $_SERVER['DOCUMENT_ROOT'].'/myfile.txt';
    $data = 'Latitude: ' . $coordinate->latitude . '\n' .
            'Longitude: ' . $coordinate->longitude;

    if (false !== file_put_contents($file, $data, FILE_APPEND))
    {
        return json_encode(array(
            'success' => true
        ));
    }

    return json_encode(array(
        'success' => false,
        'error' => 'Unable to write to file.'
    ));
}

未经测试

这是我使用的代码片段。我只取了需要的部分,但如果您需要,我可以提供其余的代码

function codeAddress(obj) {
    var address = obj.parentNode.previousSibling.innerHTML;
    geocoder.geocode( { 'address': address, 'partialmatch': true}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

            // uncomment next line to center the map
            // map.setCenter(results[0].geometry.location);
            // uncomment next line if you want to place a marker 
            // placeMarker(results[0].geometry.location, map, results[0].formatted_address);

            // save latlng and use it later             
            latlng = results[0].geometry.location.toString().replace("\(","").replace("\)","").replace(" ","");         
        } else {
            alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }

提示:
只是想让您知道,您每秒发送的查询不能超过10个。因此,如果您从文本文件中读取地址,请执行暂停:)

您可以将结果发布到php文件中,然后使用php写入文件?以前从未使用过php,听起来我不得不尝试一下。关于javascript中的“如何将结果发布到php文件”有什么想法吗?当然,你使用jquery吗?到目前为止,我可能已经写了十几行jquery语句。我能跟上。谢谢。我会试一试的。一旦这项工作开始,我将不得不从.php文件转换为.txt文件?不,php文件正在写入您的.txt文件,如第4行所示
$\u服务器['DOCUMENT\u ROOT']
指向您网站的根目录,因此此文件将写入
$data
中的内容并将其附加到
您的/website/myfile.txt
中。你可以随意改变。如果文件不存在,它将创建它。你发布的部分是我已经得到的,将结果发布到文本文件是个问题。谢谢你的邀请reply@cs2016我正在清理和复制粘贴代码,所以我没有看到其他答案。无论如何,很高兴你解决了这个问题:)