aliamjid/google-map

Google Maps 集成到 PHP / Nette 框架

1.0.4 2019-06-25 13:00 UTC

This package is auto-updated.

Last update: 2024-09-26 01:00:07 UTC


README

如何安装

将此添加到 composer.json 文件中

aliamjid/google-map": "dev-master

您还需要将 JavaScript 文件添加到您的网站上。您可以在以下位置找到它

/vendor/aliamjid/google-map/dist/GoogleMapHandler.js

它看起来会怎样?

enter image description here

完整的 nette 组件示例

use aliamjid\GoogleMap\GoogleMap;  
use aliamjid\GoogleMap\Objects\Config;  
use aliamjid\GoogleMap\Objects\DropMarkerIcon;  
use aliamjid\GoogleMap\Objects\Point;
.... use .... 
class CustomerMap extends GoogleMap {  
  private $idCustomer;  
  private $addressMapper;  
  
  public function __construct(  
  $name,  
 Container $container,  
 AddressMapper $addressMapper) {  
  $params = $container->getParameters();  
  //You need to construct parent with you Google Api Key
  parent::__construct($params['googleApiKey']); 
  //Inject your other deps. 
  $this->addressMapper = $addressMapper;  
 }  
  public function defineConfig() {  
  //You can also define config of, or can leave it empty
  $config = new Config();  
  $config->showFilters = false;  
  $this->setConfig($config);  
 }  
  public function defineFilters() {  
  //Here you can define filters
  //1st you need to create aliamjid\GoogleMap\Objects\Group
  $typeFilter = new Group('Address type');  
  //Than you can add filter to group
	$typeFilter->addFilter(  
	  'type_delivery_filter',  
	  'Delivery addresses',  
	  'address_type',  
	  'delivery'  
	);  
	//in finel step you need to register Group 
	$this->addFilterGroup($typeFilter);
 }  
 //In this method you need to define Points
 //This method is called by ajax from javascript, so its async cause of improovement of map performence
  public function definePoints() {  
 //Get your data from database
  $defaultAddress = $this->addressMapper->loadOneByCond(array('id_customer' => $this->idCustomer, 'type' => AddressType::DEFAULT_ADDRESS));  
  $deliveryAddress = $this->addressMapper->loadOneByCond(array('id_customer' => $this->idCustomer, 'type' => AddressType::DELIVERY_ADDRESS));  
  try {  
  
 //we call our custome method
  $this->addPointForAddress($defaultAddress);  
  
  $this->addPointForAddress($deliveryAddress);  
 } catch (Exception $e) {  
 } }  
 
//We create a custome method for adding address 
  private function addPointForAddress(Address $address) {
  //You need to define point 
  //Look to aliamjid\objects\Point class for more   
  $point = new Point(  
  $address->name,  
  //Cords, of point
  $address->lat,  
  $address->lng,  
  //DropMarkericon si clasic Google Map icon, you can define its color, and latter laso

  //Color is in Hex withou # symbol
  //DropMarkerIcon($symbol,$color)
  new DropMarkerIcon(),  
  '#',  
  $address->street . " " . $address->city . " " . $address->zip,  
  'This is' . AddressType::translate($address->type)  
 );  
 //Now you can data relation to point, for filtering the point
 //1st param os Key thats the "address_type" which we define in filter
 //2nd is Value if key in our case it can be delivery or default
 
 //Now if the user will turn on "delivery" filter, just point with value of delivery will show up.
 
  $point->addDataRelation('address_type','delivery');
  //In end you need to register point

  $this->addPoint($point);  
 }  
  public function setCustomer($idCustomer) {  
  $this->idCustomer = $idCustomer;  
 }}

现在我们可以在 presenter 中创建组件

public function createComponentCustomerMap() {  
  return $this->customerMap;  
}

然后您只需将其渲染到模板中

{control customerMap}

然后您的地图就准备好了。

方法和类的描述

在通过 aliamjid\GoogleMap\GoogleMap 扩展您的类之后,您需要定义 3 个必需的方法

  • DefineConfig()
  • DefinePoints()
  • DefineFilters()

定义配置

此方法并不是那么重要,您可以将其留空。但它用于定义基本地图配置。您可以 设置地图高度,或者如果您不需要,可以 隐藏过滤器

示例

public function defineConfig() {  
  $config = new Config();  
  $config->showFilters = false;  
  $config->mapHeight = 600;
  //You need to set config
  $this->setConfig($config);  
}

定义点

这是非常重要的一点。您在这里定义地图上的点。您需要为点提供经纬度、图标并定义点的描述

因此,您首先创建一个 Point。您将这样做

$point = new vendor/aliamjid/google-
map/src/GoogleMap/Objects/Point(
$name, $lat, $lng, $icon, $nameRedirect = '',$address,$additonalComment = ''
);

$lat & $lng 是点的坐标

enter image description here

如何设置图标颜色和符号? 您可以使用预创建的类

GoogleMap/Objects/DropMarkerIcon

$icon = new DropMarkerIcon($symbol,$color);

如果您想创建自己的图标,您可以这样操作

class MyCustomeIcon extends aliamjid\GoogleMap\Objects\Icon {  

public $src;  
public function __construct() {  
$this->src = "link/to/icon.png";  
}}