Index: trunk/extensions/Maps/ParserFunctions/GeoFunctions/Maps_GeoFunctions.php |
— | — | @@ -53,7 +53,7 @@ |
54 | 54 | |
55 | 55 | // Default parameter assignment, to allow for nameless syntax. |
56 | 56 | $defaultParams = array( 'location1', 'location2' ); |
57 | | - $parameters = array(); |
| 57 | + $parameters = array(); |
58 | 58 | |
59 | 59 | // Determine all parameter names and value, and take care of default (nameless) |
60 | 60 | // parameters, by turning them into named ones. |
— | — | @@ -86,41 +86,74 @@ |
87 | 87 | $doCalculation = $parameters !== false; |
88 | 88 | |
89 | 89 | if ( $doCalculation ) { |
90 | | - $start = MapsCoordinateParser::parseCoordinates( $parameters['location1'] ); |
91 | | - $end = MapsCoordinateParser::parseCoordinates( $parameters['location2'] ); |
92 | | - |
93 | | - $northRad1 = deg2rad( $start['lat'] ); |
94 | | - $eastRad1 = deg2rad( $start['lon'] ); |
95 | | - |
96 | | - $cosNorth1 = cos( $northRad1 ); |
97 | | - $cosEast1 = cos( $eastRad1 ); |
| 90 | + if ( self::geocoderIsAvailable() ) { |
| 91 | + $start = MapsGeocoder::attemptToGeocode( $parameters['location1'] ); |
| 92 | + $end = MapsGeocoder::attemptToGeocode( $parameters['location2'] ); |
| 93 | + } else { |
| 94 | + $start = MapsCoordinateParser::parseCoordinates( $parameters['location1'] ); |
| 95 | + $end = MapsCoordinateParser::parseCoordinates( $parameters['location2'] ); |
| 96 | + } |
98 | 97 | |
99 | | - $sinNorth1 = sin( $northRad1 ); |
100 | | - $sinEast1 = sin( $eastRad1 ); |
101 | | - |
102 | | - $northRad2 = deg2rad( $end['lat'] ); |
103 | | - $eastRad2 = deg2rad( $end['lon'] ); |
104 | | - |
105 | | - $cosNorth2 = cos( $northRad2 ); |
106 | | - $cosEast2 = cos( $eastRad2 ); |
107 | | - |
108 | | - $sinNorth2 = sin( $northRad2 ); |
109 | | - $sinEast2 = sin( $eastRad2 ); |
110 | | - |
111 | | - $term1 = $cosNorth1 * $sinEast1 - $cosNorth2 * $sinEast2; |
112 | | - $term2 = $cosNorth1 * $cosEast1 - $cosNorth2 * $cosEast2; |
113 | | - $term3 = $sinNorth1 - $sinNorth2; |
114 | | - |
115 | | - $distThruSquared = $term1 * $term1 + $term2 * $term2 + $term3 * $term3; |
116 | | - |
117 | | - $surfaceDistance = 2 * Maps_EARTH_RADIUS * asin( sqrt( $distThruSquared ) / 2 ); |
118 | | - |
119 | | - $output = $surfaceDistance . ' km<br />' . $manager->getErrorList(); |
| 98 | + if ( $start && $end ) { |
| 99 | + $northRad1 = deg2rad( $start['lat'] ); |
| 100 | + $eastRad1 = deg2rad( $start['lon'] ); |
| 101 | + |
| 102 | + $cosNorth1 = cos( $northRad1 ); |
| 103 | + $cosEast1 = cos( $eastRad1 ); |
| 104 | + |
| 105 | + $sinNorth1 = sin( $northRad1 ); |
| 106 | + $sinEast1 = sin( $eastRad1 ); |
| 107 | + |
| 108 | + $northRad2 = deg2rad( $end['lat'] ); |
| 109 | + $eastRad2 = deg2rad( $end['lon'] ); |
| 110 | + |
| 111 | + $cosNorth2 = cos( $northRad2 ); |
| 112 | + $cosEast2 = cos( $eastRad2 ); |
| 113 | + |
| 114 | + $sinNorth2 = sin( $northRad2 ); |
| 115 | + $sinEast2 = sin( $eastRad2 ); |
| 116 | + |
| 117 | + $term1 = $cosNorth1 * $sinEast1 - $cosNorth2 * $sinEast2; |
| 118 | + $term2 = $cosNorth1 * $cosEast1 - $cosNorth2 * $cosEast2; |
| 119 | + $term3 = $sinNorth1 - $sinNorth2; |
| 120 | + |
| 121 | + $distThruSquared = $term1 * $term1 + $term2 * $term2 + $term3 * $term3; |
| 122 | + |
| 123 | + $surfaceDistance = 2 * Maps_EARTH_RADIUS * asin( sqrt( $distThruSquared ) / 2 ); |
| 124 | + |
| 125 | + $output = $surfaceDistance . ' km'; |
| 126 | + $errorList = $manager->getErrorList(); |
| 127 | + |
| 128 | + if ( $errorList != '' ) { |
| 129 | + $output .= '<br />' . $errorList; |
| 130 | + } |
| 131 | + } else { |
| 132 | + $errorList = ''; |
| 133 | + |
| 134 | + if ( !$start ) { |
| 135 | + $errorList .= wfMsgExt( 'maps-invalid-coordinates', array( 'parsemag' ), $parameters['location1'] ); |
| 136 | + } |
| 137 | + |
| 138 | + if ( !$end ) { |
| 139 | + if ( $errorList != '' ) $errorList .= '<br />'; |
| 140 | + $errorList .= wfMsgExt( 'maps-invalid-coordinates', array( 'parsemag' ), $parameters['location2'] ); |
| 141 | + } |
| 142 | + |
| 143 | + $output = $errorList; |
| 144 | + } |
120 | 145 | } else { |
121 | 146 | $output = $manager->getErrorList(); |
122 | 147 | } |
123 | 148 | |
124 | 149 | return array( $output, 'noparse' => true, 'isHTML' => true ); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Returns a boolean indicating if MapsGeocoder is available. |
| 154 | + */ |
| 155 | + private static function geocoderIsAvailable() { |
| 156 | + global $wgAutoloadClasses; |
| 157 | + return array_key_exists( 'MapsGeocoder', $wgAutoloadClasses ); |
125 | 158 | } |
126 | 159 | |
127 | 160 | } |
Index: trunk/extensions/Maps/Geocoders/Maps_Geocoder.php |
— | — | @@ -31,20 +31,20 @@ |
32 | 32 | * |
33 | 33 | * @param string $coordsOrAddress |
34 | 34 | * @param string $geoservice |
35 | | - * @param string $service |
| 35 | + * @param string $mappingService |
36 | 36 | * @param boolean $checkForCoords |
37 | 37 | * |
38 | 38 | * @return array or false |
39 | 39 | */ |
40 | | - public static function attemptToGeocode( $coordsOrAddress, $geoservice, $service, $checkForCoords = true ) { |
| 40 | + public static function attemptToGeocode( $coordsOrAddress, $geoservice = '', $mappingService = false, $checkForCoords = true ) { |
41 | 41 | if ( $checkForCoords ) { |
42 | 42 | if ( MapsCoordinateParser::areCoordinates( $coordsOrAddress ) ) { |
43 | 43 | return MapsCoordinateParser::parseCoordinates( $coordsOrAddress ); |
44 | 44 | } else { |
45 | | - return self::geocode( $coordsOrAddress, $geoservice, $service ); |
| 45 | + return self::geocode( $coordsOrAddress, $geoservice, $mappingService ); |
46 | 46 | } |
47 | 47 | } else { |
48 | | - return self::geocode( $coordsOrAddress, $geoservice, $service ); |
| 48 | + return self::geocode( $coordsOrAddress, $geoservice, $mappingService ); |
49 | 49 | } |
50 | 50 | } |
51 | 51 | |
— | — | @@ -75,7 +75,7 @@ |
76 | 76 | * |
77 | 77 | * @return array with coordinates or false |
78 | 78 | */ |
79 | | - public static function geocode( $address, $service, $mappingService ) { |
| 79 | + public static function geocode( $address, $service = '', $mappingService = false ) { |
80 | 80 | global $egMapsGeoServices, $wgAutoloadClasses, $egMapsDir, $IP, $egMapsEnableGeoCache; |
81 | 81 | |
82 | 82 | // If the adress is already in the cache and the cache is enabled, return the coordinates. |