Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PetraMotz/3bd26ad243df5c7434adb7a91c01acf1 to your computer and use it in GitHub Desktop.
Save PetraMotz/3bd26ad243df5c7434adb7a91c01acf1 to your computer and use it in GitHub Desktop.
PHP #openstreetmap #nominatim #lat #long #php
$markers = $this->markerRepository->findAll(); (vorher müssen die Marker mit Adresse usw in die DB Tabelle impportiert werden über PHP my admin)
PID muss gesetzt werden!
in den ensprechenden controller in die methode wo man die marker braucht:
foreach ($markers as $marker) {
if (!empty($marker->getLatitude()) || !empty($marker->getLongitude())) {
continue;
}
$address = urlencode($marker->getAddress());
$search = "https://nominatim.openstreetmap.org/search.php?q={$address}&format=json";
$json = $this->getSslPage($search);
$json = json_decode($json);
$lat = $json[0]->lat ?? 0; (latidude oder 0)
$long = $json[0]->lon ?? 0;
$marker->setLatitude($lat);
$marker->setLongitude($long);
$this->markerRepository->update($marker);
$this->objectManager->get(PersistenceManager::class)->persistAll();
}
eigene funktion im controller:
private function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment