Question

I have an application that stores twitter users' location in the database (from the API GET/search tweets - location). However the problem here is that it takes whatever the user has entered in his profile. Sample data of what is already included in my database:

All over the world & London
Boca Raton, Florida
Sunway Damansara, Malaysia
Munich
Brazil
40.769549,-73.993696
Long Island City, NY USA
United States
USA-Japan

As you can see there is a great variation (+junk info e.g. "Location: in my world") and that creates a big issue when I try to handle this data. What I want to do is have a script that will actually parse the values and update every row to a country (ex. Munich would change to Germany, NY could change to United States - since its only a prototype application, not a product). I am doing everything in PHP & MySQL.

Are there any suggestions on how to tackle this issue?

UPDATE

I found this example, which is sort of doing what I would like, but it does not work for every value. For instance when I enter London or Athens it shows United States, however when I enter Munich it shows Germany... Any idea why?

<?php
// Get STATE from Google GeoData
function reverse_geocode($address) {
    $address = str_replace(" ", "+", "$address");
    $url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false";
    $result = file_get_contents("$url");
    $json = json_decode($result);
    foreach ($json->results as $result)
    {
        foreach($result->address_components as $addressPart) {
            if((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types)))
                $city = $addressPart->long_name;
            else if((in_array('administrative_area_level_1', $addressPart->types)) && (in_array('political', $addressPart->types)))
                $state = $addressPart->long_name;
            else if((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types)))
                $country = $addressPart->long_name;
        }
    }

    if(($city != '') && ($state != '') && ($country != ''))
        $address = $city.', '.$state.', '.$country;
    else if(($city != '') && ($state != ''))
        $address = $city.', '.$state;
    else if(($state != '') && ($country != ''))
        $address = $state.', '.$country;
    else if($country != '')
        $address = $country;

    // return $address;
    return "$country/$state/$city";
}

// Usage: In my case, I needed to return the State and Country of an address
$myLocation = reverse_geocode("Athens");
echo "$myLocation";
?>

Actually there is a priority on searching first in the US. So it shows the place called Athens and London in the US instead of Greece and United Kingdom. But how can someone change that? Is it even possible?

Was it helpful?

Solution

Since I found a solution to my problem I post here also the code solving the problem above from the code posted. I could get the value of city, state, country in first iteration so it was performing unnecessary iterations and therefore getting results from US as I saw in the json returned. Now I will also parse the data to make junk data such as "in my world etc" excluded and I will be able to group them by country.

<?php
// Get STATE from Google GeoData
function reverse_geocode($address) {
    $address = str_replace(" ", "+", "$address");
    $url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false";
    $result = file_get_contents("$url");
    $json = json_decode($result);
    foreach ($json->results as $k => $result)
    {
        if ($k == 0) {
            foreach($result->address_components as $addressPart) {
                if ((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types))) {
                    $city = $addressPart->long_name;
                } else if((in_array('administrative_area_level_1', $addressPart->types)) && (in_array('political', $addressPart->types))) {
                    $state = $addressPart->long_name;
                }
                else if((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types))) {
                    $country = $addressPart->long_name;
                }
            }
        } else {
            break; //Will comes out of loop directly after getting result.
        }
    }

    if($country != '')
        $address = $country;

    // return $address;
    return "$country";
}

// Usage: In my case, I needed to return the State and Country of an address
$myLocation = reverse_geocode("Athens");
echo "$myLocation";
?>

@JaredFarrish thanks for the help you provided in the first part :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top