我一直在使用谷歌地图和codeigniter做一些项目 我尝试使用谷歌地图库进行codeigniter 我正在尝试从存储在我的数据库中的纬度和经度绘制折线 当使用标记时,它完美地完成但是当我使用折线时,绘图是无法控制的,有人可以给一些指针吗?
这是我一直在使用的代码 控制器:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Map extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
// Load the library
$this->load->library('googlemaps');
// Load our model
$this->load->model('map_model', '', TRUE);
// Initialize the map, passing through any parameters
$config['center'] = '7,112';
$config['zoom'] = "auto";
$this->googlemaps->initialize($config);
// Get the co-ordinates from the database using our model
$coords = $this->map_model->get_coordinates();
$x=0;
// Loop through the coordinates we obtained above and add them to the map
foreach ($coords as $coordinate)
{
$x++;
if($x%2==1)
{
$elat=$coordinate->lat;
$elong=$coordinate->long;
/*print $elat;
print $elong;*/
print '<br>';
}
if($x%2==0)
{
$lat=$coordinate->lat;
$long=$coordinate->long;
/*print $lat;
print $long;*/
print '<br>';
}
if($x>1&&$x<6)
{
if($x%2==0)
{
$polyline=array();
$polyline['points']=array('$elat,$elong','$lat,$long');
$this->googlemaps->add_polyline($polyline);
print $x;
print '<br>';
print "(";
print $elat;
print $elong;
print '<br>';
print $lat;
print $long;
print ")";
}
if($x%2==1)
{
$polyline=array();
$polyline['points']=array('$lat,$long','$elat,$elong');
$this->googlemaps->add_polyline($polyline);
print $x;
print '<br>';
print "(";
print $elat;
print $elong;
print '<br>';
print $lat;
print $long;
print ")";
}
}
$marker = array();
$marker['position'] = $coordinate->lat.','.$coordinate->long;
$this->googlemaps->add_marker($marker);
}
/*foreach ($coords as $coordinate)
{
$marker = array();
$marker['position'] = $coordinate->lat.','.$coordinate->long;
$this->googlemaps->add_marker($marker);
$polyline=array();
$polyline['points']=array($coordinate->lat,$coordinate->long.',');
$this->googlemaps->add_polyline($polyline);
}*/
// Create the map
$data = array();
$data['map'] = $this->googlemaps->create_map();
// Load our view, passing through the map data
$this->load->view('map_view', $data);
}
}
观点:
<html>
<head><?php echo $map['js']; ?></head>
<body><?php echo $map['html']; ?></body>
</html>
型号:
<?php
class Map_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function get_coordinates()
{
$return = array();
$this->db->select("lat,long");
$this->db->from("track");
$query = $this->db->get();
if ($query->num_rows()>0) {
foreach ($query->result() as $row) {
array_push($return, $row);
}
}
return $return;
}
}