简体   繁体   中英

Getting latitude and longitude from an address and writing to a file

I have having some trouble with this one. I'm getting the impression this isn't possible using javascript.

I have a text file formatted like:

Street #    STREET  CITY    ST  ZIP  

all separated by /t, and I would like to convert this address to latitude and longitude coordinates.

So the idea is to read a line from a text file, and write to either than same local file or to a whole new file if that is easier. I only have to do this once, Just one single pass of converting addresses to latitude and longitude. Will not be in the application just need for my own use a list of latitude and longitude (around 200 addresses to be converted)

I am not sure if there is anyway to do this without using Google Maps API, but I know I can retrieve the latitude and longitude by:

        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);
            }
       });

So not only do I not know if its even possible to write to a file using js in the browser, but I also know that the function above is run asynchronous and may mess it up that way.

Any idea? If this is doable in another language that will be easier?

Thanks

Assuming you are using 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);
    }
});

Now, 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.'
    ));
}

Untested

This is the snippet I use. I took only the needed part but if you need I could provide the rest of code.

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);
      }
    });
  }

Tip: Just to let you know that you cant sent more than ten queries per second. So, if you read addresses from a text file, implement a pause :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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