Skip to content

Commit

Permalink
Merge pull request #33 from dshanske/map_style
Browse files Browse the repository at this point in the history
Add Styles to Maps
  • Loading branch information
dshanske committed Aug 20, 2017
2 parents 633892f + 4420087 commit 24206fe
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 60 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Simple Location #
**Contributors:** dshanske
**Tags:** geolocation, geo, maps, location, indieweb
**Stable tag:** 3.2.0
**Stable tag:** 3.2.1
**Requires at least:** 4.7
**Tested up to:** 4.8
**Tested up to:** 4.8.1
**License:** GPLv2 or later
**License URI:** http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -50,6 +50,10 @@ will now be required to show maps for services that require API keys.

## Changelog ##

### Version 3.2.1 ###
* Show settings for current default map provider only
* Add style settings for each map provider ( props @miklb )

### Version 3.2.0 ###
* Allow passing of coordinates directly in constructor for map provider
* Switch to argument array instead of individual properties for most display and data functions
Expand Down
16 changes: 15 additions & 1 deletion includes/class-geo-provider-bing.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public function __construct( $args = array() ) {
if ( ! isset( $args['api'] ) ) {
$args['api'] = get_option( 'sloc_bing_api' );
}
if ( ! isset( $args['style' ] ) ) {
$args['style'] = get_option( 'sloc_bing_style' );
}
parent::__construct( $args );
}

Expand All @@ -27,9 +30,20 @@ public function reverse_lookup( ) {
return $addr;
}

public function get_styles() {
return array(
'Aerial' => __( 'Aerial Imagery', 'simple-location' ),
'AerialWithLabels' => __( 'Aerial Imagery with a Road Overlay', 'simple-location' ),
'CanvasLight' => __( 'A lighter version of the road maps which also has some of the details such as hill shading disabled.', 'simple-location' ),
'CanvasDark' => __( 'A dark version of the road maps.', 'simple-location' ),
'CanvasGray' => __( 'A grayscale version of the road maps.', 'simple-location' ),
'Road' => __( 'Roads without additional imagery', 'simple-location' ),
);
}

// Return code for map
public function get_the_static_map( ) {
$map = 'http://dev.virtualearth.net/REST/v1/Imagery/Map/CanvasLight/' . $this->latitude . ',' . $this->longitude . '/' . $this->map_zoom . '?mapSize=' . $this->width . ',' . $this->height . '&key=' . $this->api;
$map = 'http://dev.virtualearth.net/REST/v1/Imagery/Map/' . $this->style . '/' . $this->latitude . ',' . $this->longitude . '/' . $this->map_zoom . '?mapSize=' . $this->width . ',' . $this->height . '&key=' . $this->api;
return $map;
}

Expand Down
14 changes: 13 additions & 1 deletion includes/class-geo-provider-google.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public function __construct( $args = array() ) {
if ( ! isset( $args['api'] ) ) {
$args['api'] = get_option( 'sloc_google_api' );
}
if ( ! isset( $args['style' ] ) ) {
$args['style'] = get_option( 'sloc_google_style' );
}
parent::__construct( $args );
}

Expand All @@ -27,9 +30,18 @@ public function reverse_lookup( ) {
return $addr;
}

public function get_styles() {
return array(
'roadmap' => __( 'Roadmap', 'simple-location' ),
'satellite' => __( 'Satellite', 'simple-location' ),
'terrain' => __( 'Terrain', 'simple-location' ),
'hybrid' => __( 'Satellite and Roadmap Hybrid', 'simple-location' ),
);
}

// Return code for map
public function get_the_static_map( ) {
$map = 'https://maps.googleapis.com/maps/api/staticmap?markers=color:red%7Clabel:P%7C' . $this->latitude . ',' . $this->longitude . '&size=' . $this->width . 'x' . $this->height . '&language=' . get_bloginfo( 'language' ) . '&key=' . $this->api;
$map = 'https://maps.googleapis.com/maps/api/staticmap?markers=color:red%7Clabel:P%7C' . $this->latitude . ',' . $this->longitude . '&size=' . $this->width . 'x' . $this->height . '&maptype=' . $this->style . '&language=' . get_bloginfo( 'language' ) . '&key=' . $this->api;
return $map;
}

Expand Down
54 changes: 53 additions & 1 deletion includes/class-geo-provider-osm.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ public function __construct( $args = array() ) {
$args['api'] = get_option( 'sloc_mapbox_api' );
}

if ( ! isset( $args['user'] ) ) {
$args['user'] = get_option( 'sloc_mapbox_user' );
}

if ( ! isset( $args['style'] ) ) {
$args['style'] = get_option( 'sloc_mapbox_style' );
}

parent::__construct( $args );
}

Expand Down Expand Up @@ -50,6 +58,46 @@ public function reverse_lookup() {
return $addr;
}

public function default_styles() {
return array(
'streets-v10' => 'Mapbox Streets',
'outdoors-v10' => 'Mapbox Outdoor',
'light-v9' => 'Mapbox Light',
'dark-v9' => 'Mapbox Dark',
'satellite-v9' => 'Mapbox Satellite',
'satellite-streets-v10' => 'Mapbox Satellite Streets',
'traffic-day-v2' => 'Mapbox Traffic Day',
'traffic-night-v2' => 'Mapbox Traffic Night',
);
}

public function get_styles() {
if ( empty( $this->user ) ) {
return array();
}
$return = $this->default_styles();
if ( 'mapbox' === $this->user ) {
return $return;
}
$url = 'https://api.mapbox.com/styles/v1/' . $this->user . '?access_token=' . $this->api;
$request = wp_remote_get( $url );
if ( is_wp_error( $request ) ) {
return $request; // Bail early.
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
if ( 200 !== wp_remote_retrieve_response_code( $request ) ) {
return new WP_Error( '403', $data->message );
}
foreach ( $data as $style ) {
if ( is_object( $style ) ) {
$return[ $style->id ] = $style->name;
}
}
return $return;
}


public function get_the_map_url() {
return 'http://www.openstreetmap.org/#map=14/' . $this->latitude . '/' . $this->longitude;
}
Expand All @@ -63,7 +111,11 @@ public function get_the_map( $static = true ) {
}

public function get_the_static_map( ) {
$map = 'https://api.mapbox.com/styles/v1/mapbox/streets-v8/static/' . $this->longitude . ',' . $this->latitude. ',' . $this->map_zoom . ',0,0/' . $this->width . 'x' . $this->height . '?access_token=' . $this->api;
$user = $this->user;
if ( array_key_exists( $this->style, $this->default_styles() ) ) {
$user = 'mapbox';
}
$map = 'https://api.mapbox.com/styles/v1/' . $user . '/' . $this->style . '/static/' . $this->longitude . ',' . $this->latitude. ',' . $this->map_zoom . ',0,0/' . $this->width . 'x' . $this->height . '?access_token=' . $this->api;
return $map;

}
Expand Down
12 changes: 12 additions & 0 deletions includes/class-geo-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ abstract class Geo_Provider {
protected $height;
protected $width;
protected $api;
protected $style;
protected $user;
protected $latitude;
protected $longitude;
protected $address;
Expand All @@ -31,12 +33,16 @@ public function __construct( $args = array() ) {
'latitude' => null,
'longitude' => null,
'reverse_zoom' => 18,
'user' => '',
'style' => '',
);
$defaults = apply_filters( 'sloc_geo_provider_defaults', $defaults );
$r = wp_parse_args( $args, $defaults );
$this->height = $r['height'];
$this->width = $r['width'];
$this->map_zoom = $r['map_zoom'];
$this->user = $r['user'];
$this->style = $r['style'];
$this->api = $r['api'];
$this->set( $r['latitude'], $r['longitude'] );
}
Expand Down Expand Up @@ -81,6 +87,12 @@ public function get() {
*/
abstract public function reverse_lookup();

/**
* Return an array of styles with key being id and value being display name
*
* @return array
*/
abstract public function get_styles();

/**
* Generate Display Name for a Reverse Address Lookup
Expand Down
Loading

0 comments on commit 24206fe

Please sign in to comment.