Как сохранить местоположение на карте Google в php - PullRequest
1 голос
/ 08 марта 2011

Подскажите, пожалуйста, пример "как сохранить местоположение на карте Google с помощью php?"Я создаю сайт недвижимости, и им нужно сохранить местоположение на карте Google, когда администратор добавляет новое свойство.Пожалуйста, помогите.

Заранее спасибо

Ответы [ 3 ]

1 голос
/ 08 марта 2011

Первым шагом является перевод вашего адреса в кодированное URL-адрес для использования в API геокодирования Google.Вот пример:

$Address = '';
$format = 'xml'; // Set this to xml or json
if(!empty($row['Address_1'])) $Address .= ($row['Address_1']);
if(!empty($row['Address_2'])) $Address .= ($row['Address_2']);
if(!empty($Address)) $Address .= ",+";
if(!empty($row['City'])) $Address .= ($row['City']);
if(!empty($Address)) $Address .= ",+";
if(!empty($row['State'])) $Address .= ($row['State']);
$Address = str_replace(' ', '+', $Address);
$url = 'http://maps.googleapis.com/maps/api/geocode/'.$format.'?address='.htmlentities($Address).'&sensor=false';

Далее вы захотите получить результат и сохранить его:

if($format == 'xml')
{
    $document = new DOMDocument('1.0');
    $document->load($url);

    // Check Status
    $RequestStatus = $document->getElementsByTagName("status")->item(0)->firstChild->nodeValue;
    if($RequestStatus !== 'OK')
    {
        echo("There was an error with the request. The status returned was: ".$RequestStatus);
    }

    // Grab the Geometry->Location Node
    $GeoLoc = $document->getElementsByTagName("location")->item(0);
    foreach ($GeoLoc->childNodes as $node)
    {
        if($node->nodeName == 'lat') echo "Latitude: " . $node->nodeValue . "<br />";
        if($node->nodeName == 'lng') echo "Longitude: " . $node->nodeValue . "<br />";
    }
}
else if($format == 'json')
{
    $json = file_get_contents($url,0,null,null);
    $json_output = json_decode($json, true);

    if($json_output['status'] != 'OK')
    {
        echo("There was an error with the request. The status returned was: ".$json_output['status']);
        // Set your lat/long data to 0, null, etc
    }
    foreach ($json_output['results'] as $result)
    {
        echo "Latitude: " . $result['geometry']['location']['lat'] . "<br />";
        echo "Longitude: " . $result['geometry']['location']['lng'] . "<br />";
        // Store your lat/long data in your database
    }

}
0 голосов
/ 08 марта 2011

Я решаю это следующим образом (хотя это было для GM2 и для модуля Joomla):

1.Get location name from db (which stored by admin)
2.Get the latitude and longitude of the place using getLatLng.
3.Use the latitude and longitude to plot the place in map.
0 голосов
/ 08 марта 2011
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...