Permalink
Browse files

Working my way out of this git nightmare

  • Loading branch information...
1 parent c8a7c6f commit 047626bcfdfa59b53a36b2558430e58585a169d4 @galen committed Aug 4, 2011
Showing with 78 additions and 99 deletions.
  1. +14 −36 PHPGoogleMaps/Core/LatLng.php
  2. +58 −58 PHPGoogleMaps/Map.php
  3. +5 −5 PHPGoogleMaps/Overlay/Circle.php
  4. +1 −0 examples/binding.php
@@ -3,11 +3,11 @@
namespace PHPGoogleMaps\Core;
/**
- * Lat Lng class
+ * LatLng class
* Base class used for storing lat/lngs
*/
-class LatLng {
+class LatLng extends PositionAbstract {
/**
* Latitude
@@ -28,7 +28,7 @@ class LatLng {
*
* @var string
*/
- private $location;
+ public $location;
/**
* Constructor
@@ -37,44 +37,22 @@ class LatLng {
* @param float $lng Longitude
* @return LatLng
*/
- public function __construct( $lat, $lng ) {
+ public function __construct( $lat, $lng, $location=null ) {
$this->lat = $lat;
$this->lng = $lng;
+ $this->location = $location;
}
- /**
- * Get distance from another latlng
- *
- * @param LatLng $latlng LatLng object to get distance from.
- * @param float $adjustment Adjust the distance to take turns into account.
- * 1.125 seems to be the most accurate.
- * @param string $units Units to return. Default (m) is miles.
- * n = nautical miles, k = kilometers,
- * f = feet, i = inches.
- * @return float Distance in the specified units
- */
- public function getDistanceFrom( LatLng $latlng, $adjustment=1.125, $units='m' ) {
- $miles = $adjustment * 3959 * acos( cos( deg2rad( $this->lat ) ) * cos( deg2rad( $latlng->lat ) ) * cos( deg2rad( $this->lng ) - deg2rad( $latlng->lng ) ) + sin( deg2rad($this->lat ) ) * sin( deg2rad( $latlng->lat ) ) );
- switch( strtolower( $units ) ) {
- case 'k':
- return $miles * 1.609344;
- break;
- case 'n':
- return $miles * 0.868976242;
- break;
- case 'f':
- return $miles * 5280;
- break;
- case 'i':
- return $miles * 63360;
- break;
- case 'm':
- default:
- return $miles;
- break;
- }
+ public function getLatLng() {
+ return $this;
}
-
+ public function getLat() {
+ return $this->lat;
+ }
+ public function getLng() {
+ return $this->lng;
+ }
+
/**
* Returns a string in the format lat,lng
*
View
@@ -678,8 +678,8 @@ function getStaticMap( $format='png', array $visible = null ) {
if ( is_array( $visible ) ) {
$request .= sprintf( "visible=%s&", implode( '|', $visible ) );
}
- if ( isset( $this->center->lat ) && isset( $this->center->lng ) ) {
- $request .= sprintf( "center=%s,%s&", $this->center->lat, $this->center->lng );
+ if ( $this->center instanceof \PHPGoogleMaps\Core\PositionAbstract ) {
+ $request .= sprintf( "center=%s,%s&", $this->center->getLat(), $this->center->getLng() );
$request .= sprintf( "zoom=%s&", $this->zoom );
}
foreach( $this->markers as $marker ) {
@@ -689,7 +689,7 @@ function getStaticMap( $format='png', array $visible = null ) {
isset( $marker->static->label ) ? strtoupper( (string) $marker->static->label[0] ) : '',
$marker->icon ? sprintf( 'icon:%s|', $marker->icon ) : '',
isset( $marker->static->flat ) ? ( ( $marker->static->flat ) ? 'false' : 'true' ) : '',
- $marker->position->lat, $marker->position->lng
+ $marker->position->getLat(), $marker->position->getLng()
);
}
return sprintf( "%s%s", $url, $request );
@@ -786,16 +786,16 @@ public function enableStreetView( array $options=null, $container=null ) {
$this->enableGeolocation();
$options['position'] = "geolocation";
}
- elseif ( $options['position'] instanceof \PHPGoogleMaps\Core\LatLng ) {
+ elseif ( $options['position'] instanceof \PHPGoogleMaps\Core\PositionAbstract ) {
$options['position'] = $options['position']->getLatLng();
}
else {
- $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $options['position'], true );
- if ( $geocode_result instanceof \PHPGoogleMaps\Core\LatLng ) {
- $options['position'] = $geocode_result;
+ $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $options['position'] );
+ if ( $geocode_result instanceof \PHPGoogleMaps\Core\PositionAbstract ) {
+ $options['position'] = $geocode_result->getLatLng();
}
else {
- throw new \PHPGoogleMaps\Core\GeocodeException( $geocode_result );
+ throw new \PHPGoogleMaps\Service\GeocodeException( $geocode_result );
}
}
}
@@ -1001,26 +1001,22 @@ protected function addCustomControl( \PHPGoogleMaps\Core\CustomControl $control
/**
* Set map center
*
- * @param string|LatLng $location Location of the center. Can be a
- * location or a LatLng object.
- * @return void
- * @throws GeocodeException
+ * @param string|PositionAbstract $center Location of the center. Can be a
+ * location or an object that extends PositionAbstract.
+ * @return boolean
*/
- public function setCenter( $location ) {
- if ( $location instanceof \PHPGoogleMaps\Core\LatLng ) {
- $this->center = $location;
+ public function setCenter( $center ) {
+ if ( $center instanceof \PHPGoogleMaps\Core\PositionAbstract ) {
+ $this->center = $center->getLatLng();
+ return true;
}
- else {
- $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $location );
- if ( $geocode_result instanceof \PHPGoogleMaps\Core\LatLng ) {
- $this->center = $geocode_result;
- }
- else {
- throw new \PHPGoogleMaps\Core\GeocodeException( $geocode_result );
- }
+ $geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( (string)$center );
+ if ( $geocode_result instanceof \PHPGoogleMaps\Service\GeocodeResult ) {
+ $this->center = $geocode_result->getLatLng();
+ return true;
}
+ return false;
}
-
/**
* Set map center by coordinates
*
@@ -1040,7 +1036,7 @@ public function setCenterCoords( $lat, $lng ) {
* @param string $backup_location Backup location incase geolocation fails
* @return void
*/
- public function centerOnUser( \PHPGoogleMaps\Core\LatLng $backup_location=null ) {
+ public function centerOnUser( \PHPGoogleMaps\Core\PositionAbstract $backup_location=null ) {
$this->enableGeolocation();
$this->center_on_user = true;
if ( $backup_location !== null ) {
@@ -1492,7 +1488,7 @@ public function enableMarkerStaggering( $timeout=100 ) {
* @access protected
*/
protected function addMarker( \PHPGoogleMaps\Overlay\Marker $marker ) {
- if ( !$marker->icon && $this->default_marker_icon ) {
+ if ( !$marker->getIcon() && $this->default_marker_icon ) {
$marker->setIcon( $this->default_marker_icon, $this->default_marker_shadow ?: null );
}
return $this->markers[] = new \PHPGoogleMaps\Overlay\MarkerDecorator( $marker, count( $this->markers ), $this->map_id );
@@ -1533,7 +1529,10 @@ public function setDefaultMarkerIcon( \PHPGoogleMaps\Overlay\MarkerIcon $icon, \
*/
public function getMarkerGroups() {
$this->extractMarkerData();
- return $this->marker_groups;
+ foreach( $this->marker_groups as $mg ) {
+ $groups[] = new \PHPGoogleMaps\Overlay\MarkerGroupDecorator( new \PHPGoogleMaps\Overlay\MarkerGroup( $mg['name'] ), $mg['id'], $this->map_id );
+ }
+ return $groups;
}
@@ -1544,7 +1543,6 @@ public function getMarkerGroups() {
**************************************/
/**
- * Add object
* Add an object to the map
*
* This method calls the various protected add* methods() which
@@ -1556,7 +1554,7 @@ public function getMarkerGroups() {
* @param object $object Object to add to the map
* @return object Returns a decorated object
*/
- public function addObject( &$object ) {
+ public function addObject( $object ) {
if ( !is_object( $object ) ) {
return false;
}
@@ -1604,18 +1602,20 @@ public function addObject( &$object ) {
$object = $this->addDirections( $object );
break;
default:
-
+ return false;
}
+ return $object;
}
/**
* Add an array objects to the map
*
+ * @param array $objects Array of objects to add to the map
* @return void
*/
public function addObjects( array $objects ) {
- foreach( $objects as &$object ) {
+ foreach( $objects as $object ) {
$this->addObject( $object );
}
}
@@ -1808,16 +1808,16 @@ function getMapJS() {
foreach( $this->shapes as $n => $shape ) {
if ( $shape->decoratee instanceof \PHPGoogleMaps\Overlay\Circle ) {
$output .= sprintf( "\tthis.shapes[%s] = new google.maps.Circle( {\n", $n );
- $output .= sprintf( "\t\tcenter: new google.maps.LatLng(%s,%s),\n", $shape->center->lat, $shape->center->lng );
+ $output .= sprintf( "\t\tcenter: new google.maps.LatLng(%s,%s),\n", $shape->center->getLat(), $shape->center->getLng() );
$output .= sprintf( "\t\tradius: %s,\n", $shape->radius );
}
elseif ( $shape->decoratee instanceof \PHPGoogleMaps\Overlay\Rectangle ) {
$output .= sprintf( "\tthis.shapes[%s] = new google.maps.Rectangle( {\n", $n );
$output .= sprintf( "\t\tbounds: new google.maps.LatLngBounds(new google.maps.LatLng(%s,%s),new google.maps.LatLng(%s,%s)),\n",
- $shape->southwest->lat,
- $shape->southwest->lng,
- $shape->northeast->lat,
- $shape->northeast->lng
+ $shape->southwest->getLat(),
+ $shape->southwest->getLng(),
+ $shape->northeast->getLat(),
+ $shape->northeast->getLng()
);
}
foreach( $shape->getOptions() as $var => $val ) {
@@ -1880,10 +1880,10 @@ function getMapJS() {
$request_options .= sprintf("\t\twaypoints: %s,\n", $this->parseLatLngs( $this->phptoJs( $request_value ) ) );
break;
case 'origin':
- $request_options .= sprintf( "\t\torigin: new google.maps.LatLng(%s,%s),\n", $this->directions->request_options['origin']->lat, $this->directions->request_options['origin']->lng );
+ $request_options .= sprintf( "\t\torigin: new google.maps.LatLng(%s,%s),\n", $this->directions->request_options['origin']->getLat(), $this->directions->request_options['origin']->getLng() );
break;
case 'destination':
- $request_options .= sprintf( "\t\tdestination: new google.maps.LatLng(%s,%s),\n", $this->directions->request_options['destination']->lat, $this->directions->request_options['destination']->lng );
+ $request_options .= sprintf( "\t\tdestination: new google.maps.LatLng(%s,%s),\n", $this->directions->request_options['destination']->getLat(), $this->directions->request_options['destination']->getLng() );
break;
case 'travelMode':
$request_options .= sprintf( "\t\ttravelMode: google.maps.DirectionsTravelMode.%s,\n", strtoupper( $this->directions->request_options['travelMode'] ) );
@@ -1943,9 +1943,9 @@ function getMapJS() {
if ( count( $this->marker_groups ) ) {
$output .= "\n\tthis.marker_groups = [];\n";
- $output .= "\tthis.marker_group_toggle = function( group_name ) {\n\t\tfor (i in this.marker_groups[group_name].markers) {\n\t\t\tvar marker = this.markers[this.marker_groups[group_name].markers[i]];\n\t\t\tif (marker.getVisible()) {\n\t\t\t\tmarker.setVisible( false );\n\t\t\t} else {\n\t\t\t\tmarker.setVisible( true );\n\t\t\t}\n\t\t}\n\t};\n";
+ $output .= "\tthis.marker_group_function = function( group_name, f_all, f_group ) {\n\t\tfor (i in map.markers) {\n\t\t\tvar marker = map.markers[i];\n\t\t\tf_all(marker);\n\t\t}\n\t\tfor (i in map.marker_groups[group_name].markers) {\n\t\t\tvar marker = map.markers[map.marker_groups[group_name].markers[i]];\n\t\t\tf_group(marker);\n\t\t}\n\t};\n";
foreach( $this->marker_groups as $marker_group_var => $marker_group ) {
- $output .= sprintf( "\tthis.marker_groups[\"%s\"] = {name: \"%s\", markers:[%s]};\n", $marker_group_var, $marker_group->name, implode( ',', $marker_group->_markers ) );
+ $output .= sprintf( "\tthis.marker_groups[\"%s\"] = {name: \"%s\", markers:[%s]};\n", $marker_group_var, $marker_group['name'], implode( ',', $marker_group['markers'] ) );
}
}
@@ -1969,7 +1969,7 @@ function getMapJS() {
$output .= "\t\tposition: geolocation,\n";
}
else {
- $output .= sprintf( "\t\tposition: new google.maps.LatLng(%s,%s),\n", $marker->position->lat, $marker->position->lng );
+ $output .= sprintf( "\t\tposition: new google.maps.LatLng(%s,%s),\n", $marker->position->getLat(), $marker->position->getLng() );
}
if ( !$this->clustering_js ) {
$output .= "\t\tmap: this.map,\n";
@@ -1985,10 +1985,10 @@ function getMapJS() {
}
if ( count( $marker->groups ) ) {
$gs = $this->marker_groups;
- $output .= sprintf( "\t\tgroups:[%s],\n", implode( ',', array_map( function( $g ) use ( $gs ) { return $gs[$g->var_name]->_id; }, $marker->groups ) ) );
+ $output .= sprintf( "\t\tgroups:[%s],\n", implode( ',', array_map( function( $g ) use ( $gs ) { return $gs[$g->var_name]['id']; }, $marker->groups ) ) );
}
- if ( $marker->getOption( 'animation' ) ) {
- $output .= sprintf( "\t\tanimation: %s,\n", $marker->getOption( 'animation' ) );
+ if ( $marker->animation ) {
+ $output .= sprintf( "\t\tanimation: google.maps.Animation.%s,\n", strtoupper( $marker->animation ) );
$marker->removeOption( 'animation' );
}
foreach( $marker->getOptions() as $marker_option => $marker_value ) {
@@ -2022,10 +2022,10 @@ function getMapJS() {
$output .= sprintf( "\tthis.ground_overlays[%s] = new google.maps.GroundOverlay('%s', new google.maps.LatLngBounds(new google.maps.LatLng(%s,%s),new google.maps.LatLng(%s,%s)), %s);\n\tthis.ground_overlays[%s].setMap(this.map);\n\n",
$n,
$ground_overlay->url,
- $ground_overlay->southwest->lat,
- $ground_overlay->southwest->lng,
- $ground_overlay->northeast->lat,
- $ground_overlay->northeast->lng,
+ $ground_overlay->southwest->getLat(),
+ $ground_overlay->southwest->getLng(),
+ $ground_overlay->northeast->getLat(),
+ $ground_overlay->northeast->getLng(),
$this->phpToJs( $ground_overlay->options ),
$n
);
@@ -2097,11 +2097,11 @@ function getMapJS() {
}
$output .= "\t\tthis.map.setCenter( geolocation );\n";
if ( $this->geolocation_backup ) {
- $output .= sprintf( "\t}\n\telse {\n\t\tthis.map.setCenter( new google.maps.LatLng(%s,%s) );\n\t}\n\n", $this->geolocation_backup->lat, $this->geolocation_backup->lng );
+ $output .= sprintf( "\t}\n\telse {\n\t\tthis.map.setCenter( new google.maps.LatLng(%s,%s) );\n\t}\n\n", $this->geolocation_backup->getLat(), $this->geolocation_backup->getLng() );
}
}
if ( $this->center ) {
- $output .= sprintf( "\tthis.map.setCenter( new google.maps.LatLng(%s,%s) );\n", $this->center->lat, $this->center->lng );
+ $output .= sprintf( "\tthis.map.setCenter( new google.maps.LatLng(%s,%s) );\n", $this->center->getLat(), $this->center->getLng() );
}
if ( count ($this->event_listeners ) ) {
@@ -2120,10 +2120,8 @@ function getMapJS() {
}
if ( $this->streetview ) {
-
$streetview_options = '';
-
- if ( $this->streetview->options ) {
+ if ( isset ( $this->streetview->options ) ) {
foreach( $this->streetview->options as $streetview_option => $streetview_value ) {
switch( $streetview_option ) {
case 'container':
@@ -2205,7 +2203,7 @@ private function switchquotes( $str ) {
* @access private
*/
private function parseLatLngs( $str ) {
- return preg_replace( '~\{"lat":(.*?),"lng":(.*?)\}~i', 'new google.maps.LatLng($1,$2)', $str );
+ return preg_replace( '~{"lat":(.*?),"lng":(.*?),.*?}~i', 'new google.maps.LatLng($1,$2)', $str );
}
/**
@@ -2302,14 +2300,16 @@ private function extractMarkerData() {
}
foreach ( $marker->groups as $marker_group ) {
if ( isset( $this->marker_groups[ $marker_group->var_name ] ) ) {
- $this->marker_groups[ $marker_group->var_name ]->addMarker( $marker_id );
+ $this->marker_groups[ $marker_group->var_name ]['markers'][] = $marker_id;
}
else {
- $this->marker_groups[ $marker_group->var_name ] = new \PHPGoogleMaps\Overlay\MarkerGroupDecorator( $marker_group, count( $this->marker_groups ), $this->map_id );
- $this->marker_groups[ $marker_group->var_name ]->addMarker( $marker_id );
+ $this->marker_groups[ $marker_group->var_name ] = array(
+ 'id' => count( $this->marker_groups ),
+ 'name' => $marker_group->name,
+ 'markers' => array( $marker_id )
+ );
}
}
-
}
$this->marker_data_hash = md5( serialize( $this->getMarkers() ) );
@@ -35,13 +35,13 @@ class Circle extends \PHPGoogleMaps\Overlay\Shape {
* @return Circle
*/
public function __construct( $center, $radius, array $options=null ) {
- if ( $center instanceof \PHPGoogleMaps\Core\LatLng ) {
+ if ( $center instanceof \PHPGoogleMaps\Core\PositionAbstract ) {
$this->center = $center->getLatLng();
}
else {
$geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $center, true );
- if ( $geocode_result instanceof \PHPGoogleMaps\Core\LatLng ) {
- $this->center = $geocode_result;
+ if ( $geocode_result instanceof \PHPGoogleMaps\Core\PositionAbstract ) {
+ $this->center = $geocode_result->getLatLng();
}
else {
throw new \PHPGoogleMaps\Core\GeocodeException( $geocode_result );
@@ -58,8 +58,8 @@ public static function createFromLatLng( $center, $radius, array $options=null )
public static function createFromLocation( $location, $radius, array $options=null ) {
$geocode_result = \PHPGoogleMaps\Service\Geocoder::geocode( $location );
- if ( $geocode_result instanceof \PHPGoogleMaps\Core\LatLng ) {
- return new Circle( $geocode_result, $radius, $options );
+ if ( $geocode_result instanceof \PHPGoogleMaps\Core\PositionAbstract ) {
+ return new Circle( $geocode_result->getLatLng(), $radius, $options );
}
return false;
}
Oops, something went wrong.

0 comments on commit 047626b

Please sign in to comment.