我必须在距离给定纬度/经度值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
我正在使用此网址访问我的代码:
http://localhost/ciplaces/index.php/mapcontroller
谢谢
答案 0 :(得分:4)
我有一个与Google Maps和Places API集成的CodeIgniter库。您可以在此处找到信息并下载库:
http://biostall.com/codeigniter-google-maps-v3-api-library
&#39;地方&#39;整合也可以在下面找到:
http://biostall.com/demos/google-maps-v3-api-codeigniter-library/places
如果您有任何问题或需要对图书馆进行任何更改,请给我一个喊叫,我会很乐意尽我所能帮助。
干杯
答案 1 :(得分:1)
我使用块算法在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;
}