com.cloudmade.api.geometry.Point.java Source code

Java tutorial

Introduction

Here is the source code for com.cloudmade.api.geometry.Point.java

Source

/*
 * Copyright 2009 CloudMade.
 *
 * Licensed under the GNU Lesser General Public License, Version 3.0;
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cloudmade.api.geometry;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

import org.json.JSONObject;

/**
 * Simple container of latitude, longitude pair
 * 
 * @author Mykola Paliyenko
 * 
 */
public class Point extends Geometry {
    public static final String LAT_FIELD = "lat";
    public static final String LNG_FIELD = "lng";

    /**
     * latitude of the point
     */
    public float lat;
    /**
     * longitude of the point
     */
    public float lng;

    public Point() {
        lat = 0;
        lng = 0;
    }

    public Point(double lat, double lng) {
        setPoint(lat, lng);
    }

    public void setPoint(double lat, double lng) {
        this.lat = (float) lat;
        this.lng = (float) lng;
    }

    public JSONObject toJson() {
        JSONObject json = new JSONObject();
        json.put(LAT_FIELD, this.lat);
        json.put(LNG_FIELD, this.lng);
        return json;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Point) {
            Point p = (Point) obj;
            return p.lat == lat && p.lng == lng;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return new Double(lat).hashCode() * 37 + new Double(lng).hashCode();
    }

    public float[] coords() {
        return new float[] { lat, lng };
    }

    @Override
    public String toString() {
        DecimalFormat formatter = new DecimalFormat("0.#####", new DecimalFormatSymbols(Locale.US));
        return formatter.format(lat) + "," + formatter.format(lng);
    }
}