Cakephp 地理编码行为

发布于 2024-12-18 17:35:54 字数 264 浏览 4 评论 0原文

我一直在关注本教程。基本上我想从我拥有的表格中获取一个地址并将该地址放在谷歌地图上。该教程看起来非常简单,创建桌子位置,放入行为和模型等等。

只是有点困惑我实际上如何将其与现有的表格一起使用。我已经阅读了食谱中有关行为的部分,但对它们仍然有点困惑。主要是关于如何将教程集成到不在位置模型内的其他视图和控制器中?

I have been following this tutorial. Basically I want to take an address from a table I have and place that address on a google map. The tutorial seems pretty straight forward, create the table places, put the behavior in and model and what not.

Just kind of confused how I'd actually use that with my existing tables. I've read through the section in the cookbook about behaviors but am still a little confused about them. Mostly about how I'd integrate the tutorial into other views and controllers that aren't within place model?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚你发端 2024-12-25 17:35:54

首先,您需要将纬度/经度坐标添加到地址表中...

您可以使用围绕此 google 地图 api 调用的书写解析器来完成此操作:

http://maps.google.com/maps/api/geocode/xml?address=miami&sensor=false

一旦您的地址包含坐标,你所需要的只是传递坐标
到您视图中的 javascript 谷歌地图,只需复制源代码:

http://code.google.com/apis/maps/documentation/javascript/examples/map-simple.html

这是一个 cakephp 2.0 shell,用于创建

要运行的纬度/经度数据:“cake geo”

GeoShell.php:

<?php
class GeoShell extends Shell {

public $uses = array('Business');

public function main() {



    $counter = 0;
    $unique = 0;


    $temps = $this->Business->find('all', array(
        'limit' => 100
    ));

    foreach($temps as $t) {

            $address = $this->geocodeAddress($t['Business']['address']);

            print_r($address);

            if(!empty($address)) {
                $data['Business']['id'] = $t['Business']['id'];
                $data['Business']['lat'] = $address['latitude'];
                $data['Business']['lng'] = $address['longitude'];

                $this->Business->save($data, false);
                $unique++;
            }

    }

    $counter++;


    $this->out(' - Processed Records: ' . $counter . ' - ');
    $this->out(' - Inserted Records: ' . $unique . ' - ');

}


public function geocodeAddress($address) {

    $url = 'http://maps.google.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=false';

    $response = @file_get_contents($url);

    if($response === false) {
        return false;
    }

    $response = json_decode($response);

    if($response->status != 'OK') {
        return false;
    }

    foreach ($response->results['0']->address_components as $data) {
        if($data->types['0'] == 'country') {
            $country = $data->long_name;
            $country_code = $data->short_name;
        }
    }

    $result = array(
        'latitude'  => $response->results['0']->geometry->location->lat,
        'longitude' => $response->results['0']->geometry->location->lng,
        'country' => $country,
        'country_code' => $country_code,
        'googleaddress' => $response->results['0']->formatted_address,
    );
    return $result;
}

}

first you need to add latitude / longitude coordinates to your address table...

you can do that with a writing parser around this google maps api call:

http://maps.google.com/maps/api/geocode/xml?address=miami&sensor=false

once your addresses contain the coordinates, all you need is to pass the coordinates
to a javascript google maps in your view, just copy the source :

http://code.google.com/apis/maps/documentation/javascript/examples/map-simple.html

here is a cakephp 2.0 shell to create latitude / longitude data

to run: "cake geo"

GeoShell.php :

<?php
class GeoShell extends Shell {

public $uses = array('Business');

public function main() {



    $counter = 0;
    $unique = 0;


    $temps = $this->Business->find('all', array(
        'limit' => 100
    ));

    foreach($temps as $t) {

            $address = $this->geocodeAddress($t['Business']['address']);

            print_r($address);

            if(!empty($address)) {
                $data['Business']['id'] = $t['Business']['id'];
                $data['Business']['lat'] = $address['latitude'];
                $data['Business']['lng'] = $address['longitude'];

                $this->Business->save($data, false);
                $unique++;
            }

    }

    $counter++;


    $this->out(' - Processed Records: ' . $counter . ' - ');
    $this->out(' - Inserted Records: ' . $unique . ' - ');

}


public function geocodeAddress($address) {

    $url = 'http://maps.google.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=false';

    $response = @file_get_contents($url);

    if($response === false) {
        return false;
    }

    $response = json_decode($response);

    if($response->status != 'OK') {
        return false;
    }

    foreach ($response->results['0']->address_components as $data) {
        if($data->types['0'] == 'country') {
            $country = $data->long_name;
            $country_code = $data->short_name;
        }
    }

    $result = array(
        'latitude'  => $response->results['0']->geometry->location->lat,
        'longitude' => $response->results['0']->geometry->location->lng,
        'country' => $country,
        'country_code' => $country_code,
        'googleaddress' => $response->results['0']->formatted_address,
    );
    return $result;
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文