Now getting building coordinates from Google Maps Geocoding

This commit is contained in:
Frank Schubert
2021-07-15 20:26:07 +02:00
parent e77dd8b263
commit 6555272928
5 changed files with 96 additions and 7 deletions

View File

@@ -120,21 +120,21 @@
<div class="form-group row">
<label class="col-lg-2 col-form-label" for="gps_lat">GPS Länge (N)</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="gps_lat" id="gps_lat" value="<?=$building->gps_lat?>" />
<input type="text" class="form-control" name="gps_lat" id="gps_lat" value="<?=$building->gps_lat?>" <?=(!$me->is("Admin")) ? "readonly='readonly'" : ""?> />
</div>
</div>
<div class="form-group row">
<label class="col-lg-2 col-form-label" for="gps_long">GPS Breite (E)</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="gps_long" id="gps_long" value="<?=$building->gps_long?>" />
<input type="text" class="form-control" name="gps_long" id="gps_long" value="<?=$building->gps_long?>" <?=(!$me->is("Admin")) ? "readonly='readonly'" : ""?> />
</div>
</div>
<div class="form-group row">
<label class="col-lg-2 col-form-label" for="laea">LAEA Koordinaten</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="laea" id="laea" value="<?=$building->laea?>" readonly="readonly" />
<input type="text" class="form-control" name="laea" id="laea" value="<?=$building->laea?>" <?=(!$me->is("Admin")) ? "readonly='readonly'" : ""?> />
</div>
</div>

View File

@@ -1,7 +1,7 @@
<?php
class Building extends mfBaseModel {
protected $forcestr = ['street','zip','phone','gps_lat','gps_long','email','note'];
protected $forcestr = ['street','zip','phone','email','note'];
private $network;
private $pop;

View File

@@ -70,13 +70,13 @@ class BuildingController extends mfBaseController {
$data['type_id'] = $r->type_id;
$data['status_id'] = ($r->status_id) ? $r->status_id : null;
$data['pipeworker_id'] = ($r->pipeworker_id) ? $r->pipeworker_id : null;
$data['code'] = $r->code;
$data['oan_id'] = $r->oan_id;
$data['street'] = $r->street;
$data['zip'] = $r->zip;
$data['city'] = $r->city;
$data['gps_lat'] = $r->gps_lat;
$data['gps_long'] = $r->gps_long;
$data['contact'] = $r->contact;
$data['phone'] = $r->phone;
$data['email'] = $r->email;
@@ -86,6 +86,13 @@ class BuildingController extends mfBaseController {
$data['edit_by'] = 1;
if($this->me->is("Admin")) {
if($r->gps_lat) $data['gps_lat'] = $r->gps_lat;
if($r->gps_long) $data['gps_long'] = $r->gps_long;
if($r->code) $data['code'] = $r->code;
if($r->laea) $data['laea'] = $r->laea;
}
if($mode == "add") {
$data['status_id'] = 1;
$data['create_by'] = 1;
@@ -103,6 +110,22 @@ class BuildingController extends mfBaseController {
return $this->add();
}
// get GPS location
if(!$building->gps_lat && !$building->gps_long) {
$search = [
'country' => "AT",
'city' => $building->city,
'zip' => $building->zip,
'street' => $building->street
];
$coords = Gmaps_Geocoding::getCoords($search);
if(is_array($coords) && count($coords) == 2) {
$building->gps_lat = str_replace(",",".",$coords[0]);
$building->gps_long = str_replace(",",".",$coords[1]);
$building->save();
}
}
// generate object code and LAEA coords
if(!$building->code) {
$building->code = $building->getNewObjectCode();

View File

@@ -13,4 +13,17 @@ class DashboardController extends mfBaseController {
protected function indexAction() {
}
protected function testAction() {
$search = [
'street' => "Kastellfeldgasse 20",
'zip' => "8010",
'city' => "Graz",
'country' => "AT"
];
$coords = Gmaps_Geocoding::getCoords($search);
var_dump($coords);
exit;
}
}

53
lib/Gmaps/Geocoding.php Normal file
View File

@@ -0,0 +1,53 @@
<?php
class Gmaps_Geocoding {
public static function getCoords($search) {
$log = mfLoghandler::singleton();
$components = [];
if(is_array($search)) {
if(!$search['country'] || !$search['street'] || !$search['zip'] || !$search['city']) {
return false;
}
$components = [
'country:' . urlencode($search['country']),
'postal_code:' . urlencode($search['zip']),
'locality:' . urlencode($search['city']),
];
$address = $search['street'];
} else {
$address = $search;
}
$address = urlencode($address);
$url = TT_GEOCODING_API_URL."?address=$address&key=".TT_GEOCODING_API_SECRET;
$cstr = "";
if(count($components)) {
$component_string = implode("|", $components);
$url .= "&components=$component_string";
}
$log->debug(__FILE__.": $url");
$resp = file_get_contents($url);
//print_r($resp);
if($resp) {
$json = json_decode($resp);
$results = $json->results;
if(count($results) > 1) {
$log->warn(__FILE__."> Got more then 1 result. Aborting.");
return false;
}
$lat = $results[0]->geometry->location->lat;
$long = $results[0]->geometry->location->lng;
return [$lat,$long];
} else {
return false;
}
}
}