1

我必须获取给定纬度/经度值 2 公里范围内附近位置的结果。必须使用 Google Places API 来完成。详细信息在这里:

http://code.google.com/apis/maps/documentation/javascript/places.html

他们在 javascript 中提供了示例代码。但我需要在 php 中有这个。谁能给我任何想法我该如何实现它?或者我如何在我的 php 控制器类中使用相同的 javascript 代码?[我正在使用代码点火器框架]。我已经在这个问题上停留了这么多小时。如果有人可以提供示例 php 代码,那就太好了。非常感谢任何帮助。

这是我的控制器类的代码:

<?php

class Welcome extends CI_Controller {
    public function index()
    {

    $config = "";

    //$this->load->library('googlemaps');
        $this->load->library('googlemaps');

    $config['center'] = '37.4419, -122.1419';
    $config['zoom'] = 'auto';
    $config['places'] = TRUE;
    $config['placesLocation'] = '37.4419, -122.1419';
    $config['placesRadius'] = 200; 
    $this->googlemaps->initialize($config);

    $data['map'] = $this->googlemaps->create_map();

    $this->load->view('map_view', $data);
    }
}
?>

这是我在尝试运行上述代码时遇到的错误:

致命错误:在第 9 行的 /Applications/XAMPP/xamppfiles/htdocs/ciplaces/application/controllers/mapcontroller.php 的对象上下文中使用 $this

我正在使用此 url 访问我的代码:

http://localhost/ciplaces/index.php/mapcontroller

谢谢

4

2 回答 2

4

我有一个与 Google Maps and Places API 集成的 CodeIgniter 库。您可以在此处找到信息并下载库:

http://biostall.com/codeigniter-google-maps-v3-api-library

'Places' 集成的演示也可以在下面找到:

http://biostall.com/demos/google-maps-v3-api-codeigniter-library/places

如果您有任何问题或需要对库进行任何更改,请给我留言,我很乐意尽我所能提供帮助。

干杯

于 2012-02-22T16:14:16.753 回答
1

我使用 Lumb 算法在 PHP 中做了一些类似的事情。

你应该能够从下面的代码中得到一些东西(位于我的模型中,但你可以放在任何地方)。

public function search($start_latitude, $start_longitude, $radius, $radius_type, $offset, $limit)
{
    $results = array();
    $locations = array();

    $sql = "SELECT `location_id`, `latitude`, `longitude` FROM `table`";
    $query = $this->db->query($sql);
    if ($query->num_rows() > 0)
    {
        foreach ($query->result() as $row)
        {
            $geo_data = $this->_bearing_distance_calc($start_latitude, $start_longitude, $row->latitude, $row->longitude, $radius_type);
            $geo_data['radius_type'] = $radius_type;
            if($geo_data['distance'] <= $radius)
            {
                // radial serach results
                $locations[] = $row->location_id;
            }
        }

        // return amount requested
        $results['total'] = count($locations);
        $results['locations'] = array_slice($locations, $offset, $limit);
        return $results;
    }
    else
    {
        // no results
        return FALSE;
    }
}

/**
 * Calculate Distance Between two points.
 * 
 * This method is used to calculate the distance between to geographical points. <br />
 * Used by the search method.
 * 
 * @access private
 * 
 * @param float $device_latitude
 * @param float $device_longitude
 * @param float $beach_latitude
 * @param float $beach_longitude
 * @param integer $radius_type
 * 
 * @return array 
 */
private function _bearing_distance_calc($start_latitude, $start_longitude, $building_latitude, $building_longitude, $radius_type)
{
    // using Rhumb lines(or loxodrome)
    // convert to rads for php trig functions
    $start_latitude = deg2rad($start_latitude);
    $start_longitude = deg2rad($start_longitude);
    $building_latitude = deg2rad($building_latitude);
    $building_longitude = deg2rad($building_longitude);

    // testing variables
    //$start_latitude = deg2rad(39.4422);
    //$start_longitude = deg2rad(-122.0307);
    //$building_latitude = deg2rad(49.4422);
    //$building_longitude = deg2rad(-112.0307);

    // calculate delta of lat and long
    $delta_latitude = $building_latitude-$start_latitude;
    $delta_longitude = $building_longitude-$start_longitude;

    // earth radius
    if ($radius_type == 'miles') // using miles
    {
        $earth_radius = 3959;
    }
    else // using kilometers
    {
        $earth_radius = 6371;
    }

    // now lets start mathing !!
    // cast types
    $dPhi = log(tan($building_latitude/2+M_PI/4)/tan($start_latitude/2+M_PI/4));
    if ($dPhi != 0)
    {
        $q = $delta_latitude/$dPhi;
    }
    else
    {
        $q = cos($start_latitude);
    }
    //$q = (!is_nan($delta_latitude/$dPhi)) ? $delta_latitude/$dPhi : cos($start_latitude);  // E-W line gives dPhi=0   
    // if dLon over 180° take shorter rhumb across 180° meridian:
    if (abs($delta_longitude) > M_PI)
    {
        $delta_longitude = $delta_longitude>0 ? -(2*M_PI-$delta_longitude) : (2*M_PI+$delta_longitude);
    }

    $geo_data = array();
    $geo_data['distance'] = sqrt($delta_latitude*$delta_latitude + $q*$q*$delta_longitude*$delta_longitude) * $earth_radius;
    $bearing = rad2deg(atan2($delta_longitude, $dPhi));

    if($bearing < 0)
    {
        $bearing = 360 + $bearing;
    }
    $geo_data['bearing'] = $bearing;

    return $geo_data;
}
于 2012-02-22T16:08:31.773 回答