/* Template Name: Anfahrt */ $context = Timber::get_context(); $post = new TimberPost(); $context['post'] = $post; $adr_title = get_field('firmenbezeichnung', 'option'); $adr_arr = array(); $adr_arr[] = get_field('strasse_hausnummer', 'option'); $adr_arr[] = get_field('postleitzahl', 'option'); $adr_arr[] = get_field('ort', 'option'); $adr_arr[] = get_field('bundesland', 'option'); $adr_arr[] = get_field('land', 'option'); $adr_string = implode(' ', $adr_arr); $mymap = new Mappress_Map(); $address = $adr_string; $mypoi_1 = new Mappress_Poi(array("title" => $adr_title, "address" => $address)); $mypoi_1->geocode(); $mymap->pois = array($mypoi_1); $mymap->width = '100%'; $mymap->height = 480; $mymap->options->adaptive = true; $mymap->options->alignment = 'default'; $mymap->options->border['style'] = ''; $mymap->options->directions = 'google'; $mymap->options->initialOpenInfo = true; $mymap->options->onLoad = true; $mymap->options->postTypes = array(); $context['map'] = $mymap; Timber::render(array('page-anfahrt.twig', 'page.twig'), $context);
/** * Geocode an address using http * * @param mixed $auto true = automatically update the poi, false = return raw geocoding results * @return true if auto=true and success | raw geocoding results if auto=false | WP_Error on failure */ function geocode($auto = true) { // If point was defined using only lat/lng then no geocoding if (!empty($this->point['lat']) && !empty($this->point['lng'])) { // Default title if empty if (empty($this->title)) { $this->title = $this->point['lat'] . ',' . $this->point['lng']; } return; } $options = Mappress_Options::get(); $language = $options->language; $country = $options->country; $address = urlencode($this->address); $url = "http://maps.google.com/maps/api/geocode/json?address={$address}&sensor=false&output=json"; if ($country) { $url .= "®ion={$country}"; } if ($language) { $url .= "&language={$language}"; } $response = wp_remote_get($url); // If auto=false, then return the RAW result if (!$auto) { return $response; } // Check for http error if (is_wp_error($response)) { return $response; } if (!$response) { return new WP_Error('geocode', sprintf(__('No geocoding response from Google: %s', 'mappress'), $response)); } //Decode response and automatically use first address $response = json_decode($response['body']); // Discard empty results foreach ((array) $response->results as $key => $result) { if (empty($result->formatted_address)) { unset($response->results[$key]); } } $status = isset($response->status) ? $response->status : null; if ($status != 'OK') { return new WP_Error('geocode', sprintf(__("Google cannot geocode address: %s, status: %s", 'mappress'), $this->address, $status)); } if (!$response || !isset($response->results) || empty($response->results[0]) || !isset($response->results[0])) { return new WP_Error('geocode', sprintf(__("No geocoding result for address: %s", 'mappress'), $this->address)); } $placemark = $response->results[0]; // Point $this->point = array('lat' => $placemark->geometry->location->lat, 'lng' => $placemark->geometry->location->lng); // Viewport $this->viewport = array('sw' => array('lat' => $placemark->geometry->viewport->southwest->lat, 'lng' => $placemark->geometry->viewport->southwest->lng), 'ne' => array('lat' => $placemark->geometry->viewport->northeast->lat, 'lng' => $placemark->geometry->viewport->northeast->lng)); // Corrected address $this->correctedAddress = $placemark->formatted_address; $parsed = Mappress_Poi::parse_address($this->correctedAddress); // If the title and body are not populated then default them if (!$this->title && !$this->body) { $this->title = $parsed[0]; if ($parsed[1]) { $this->body = $parsed[1]; } } }