1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * @requires OpenLayers/Console.js
10 * Class: OpenLayers.LonLat
11 * This class represents a longitude and latitude pair
13 OpenLayers.LonLat = OpenLayers.Class({
17 * {Float} The x-axis coodinate in map units
23 * {Float} The y-axis coordinate in map units
28 * Constructor: OpenLayers.LonLat
29 * Create a new map location.
32 * lon - {Number} The x-axis coordinate in map units. If your map is in
33 * a geographic projection, this will be the Longitude. Otherwise,
34 * it will be the x coordinate of the map location in your map units.
35 * lat - {Number} The y-axis coordinate in map units. If your map is in
36 * a geographic projection, this will be the Latitude. Otherwise,
37 * it will be the y coordinate of the map location in your map units.
39 initialize: function(lon, lat) {
40 this.lon = OpenLayers.Util.toFloat(lon);
41 this.lat = OpenLayers.Util.toFloat(lat);
46 * Return a readable string version of the lonlat
49 * {String} String representation of OpenLayers.LonLat object.
50 * (ex. <i>"lon=5,lat=42"</i>)
53 return ("lon=" + this.lon + ",lat=" + this.lat);
57 * APIMethod: toShortString
60 * {String} Shortened String representation of OpenLayers.LonLat object.
61 * (ex. <i>"5, 42"</i>)
63 toShortString:function() {
64 return (this.lon + ", " + this.lat);
71 * {<OpenLayers.LonLat>} New OpenLayers.LonLat object with the same lon
75 return new OpenLayers.LonLat(this.lon, this.lat);
86 * {<OpenLayers.LonLat>} A new OpenLayers.LonLat object with the lon and
87 * lat passed-in added to this's.
89 add:function(lon, lat) {
90 if ( (lon == null) || (lat == null) ) {
91 var msg = OpenLayers.i18n("lonlatAddError");
92 OpenLayers.Console.error(msg);
95 return new OpenLayers.LonLat(this.lon + lon, this.lat + lat);
102 * ll - {<OpenLayers.LonLat>}
105 * {Boolean} Boolean value indicating whether the passed-in
106 * <OpenLayers.LonLat> object has the same lon and lat
107 * components as this.
108 * Note: if ll passed in is null, returns false
110 equals:function(ll) {
113 equals = ((this.lon == ll.lon && this.lat == ll.lat) ||
114 (isNaN(this.lon) && isNaN(this.lat) && isNaN(ll.lon) && isNaN(ll.lat)));
120 * APIMethod: transform
121 * Transform the LonLat object from source to dest. This transformation is
122 * *in place*: if you want a *new* lonlat, use .clone() first.
125 * source - {<OpenLayers.Projection>} Source projection.
126 * dest - {<OpenLayers.Projection>} Destination projection.
129 * {<OpenLayers.LonLat>} Itself, for use in chaining operations.
131 transform: function(source, dest) {
132 var point = OpenLayers.Projection.transform(
133 {'x': this.lon, 'y': this.lat}, source, dest);
140 * APIMethod: wrapDateLine
143 * maxExtent - {<OpenLayers.Bounds>}
146 * {<OpenLayers.LonLat>} A copy of this lonlat, but wrapped around the
147 * "dateline" (as specified by the borders of
150 wrapDateLine: function(maxExtent) {
152 var newLonLat = this.clone();
156 while (newLonLat.lon < maxExtent.left) {
157 newLonLat.lon += maxExtent.getWidth();
161 while (newLonLat.lon > maxExtent.right) {
162 newLonLat.lon -= maxExtent.getWidth();
169 CLASS_NAME: "OpenLayers.LonLat"
173 * Function: fromString
174 * Alternative constructor that builds a new <OpenLayers.LonLat> from a
178 * str - {String} Comma-separated Lon,Lat coordinate string.
179 * (ex. <i>"5,40"</i>)
182 * {<OpenLayers.LonLat>} New <OpenLayers.LonLat> object built from the
185 OpenLayers.LonLat.fromString = function(str) {
186 var pair = str.split(",");
187 return new OpenLayers.LonLat(parseFloat(pair[0]),
188 parseFloat(pair[1]));