Android Open Source - smartnavi Kml Ground Overlay






From Project

Back to project page smartnavi.

License

The source code is released under:

Apache License

If you think the Android project smartnavi listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.osmdroid.bonuspack.kml;
/*  w  ww.  j a v  a2s  .  c  om*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.JsonObject;

import org.osmdroid.bonuspack.overlays.GroundOverlay;
import org.osmdroid.bonuspack.utils.BonusPackHelper;
import org.osmdroid.util.BoundingBoxE6;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Overlay;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * KML GroundOverlay.
 * <p/>
 * mCoordinates contains the LatLonBox as 2 GeoPoints: North-West, and South-East.
 *
 * @author M.Kergall
 */
public class KmlGroundOverlay extends KmlFeature implements Cloneable, Parcelable {
    public static final Parcelable.Creator<KmlGroundOverlay> CREATOR = new Parcelable.Creator<KmlGroundOverlay>() {
        @Override
        public KmlGroundOverlay createFromParcel(Parcel source) {
            return new KmlGroundOverlay(source);
        }

        @Override
        public KmlGroundOverlay[] newArray(int size) {
            return new KmlGroundOverlay[size];
        }
    };
    /**
     * Overlay Icon href
     */
    public String mIconHref;
    /**
     * Overlay Icon bitmap (can be null)
     */
    public Bitmap mIcon;
    /**
     * Overlay color
     */
    public int mColor;
    /**
     * GroundOverlay rotation - default = 0
     */
    public float mRotation;
    /**
     * NW and SE points - TODO: not the simplest way to handle that...
     */
    public ArrayList<GeoPoint> mCoordinates;

    public KmlGroundOverlay() {
        super();
        mColor = 0xFF000000;
    }

    /**
     * Constructs the KML feature from a GroundOverlay.
     */
    public KmlGroundOverlay(GroundOverlay overlay) {
        this();
        GeoPoint p = overlay.getPosition();
        GeoPoint pN = p.destinationPoint(overlay.getHeight() / 2, 0.0f);
        GeoPoint pS = p.destinationPoint(overlay.getHeight() / 2, 180.0f);
        GeoPoint pE = p.destinationPoint(overlay.getWidth() / 2, 90.0f);
        GeoPoint pW = p.destinationPoint(overlay.getWidth() / 2, -90.0f);
        mCoordinates = new ArrayList<GeoPoint>(2);
        mCoordinates.add(new GeoPoint(pN.getLatitudeE6(), pW.getLongitudeE6())); //NW
        mCoordinates.add(new GeoPoint(pS.getLatitudeE6(), pE.getLongitudeE6())); //SE
        //mIconHref = ???
        mIcon = ((BitmapDrawable) overlay.getImage()).getBitmap();
        mRotation = -overlay.getBearing();
        mColor = 255 - Color.alpha((int) (overlay.getTransparency() * 255));
        mVisibility = overlay.isEnabled();
    }

    public KmlGroundOverlay(Parcel in) {
        super(in);
        mIconHref = in.readString();
        mIcon = in.readParcelable(Bitmap.class.getClassLoader());
        mColor = in.readInt();
        mRotation = in.readFloat();
        mCoordinates = in.readArrayList(GeoPoint.class.getClassLoader());
    }

    @Override
    public BoundingBoxE6 getBoundingBox() {
        return BoundingBoxE6.fromGeoPoints(mCoordinates);
    }

    /**
     * load the icon from its href.
     *
     * @param href              either the full url, or a relative path to a local file.
     * @param containerFullPath full path of the container file.
     * @param kmzContainer      current KMZ file (as a ZipFile) - or null if irrelevant.
     */
    public void setIcon(String href, String containerFullPath, ZipFile kmzContainer) {
        mIconHref = href;
        if (mIconHref.startsWith("http://") || mIconHref.startsWith("https://")) {
            mIcon = BonusPackHelper.loadBitmap(mIconHref);
        } else if (kmzContainer == null) {
            File file = new File(containerFullPath);
            String actualFullPath = file.getParent() + '/' + mIconHref;
            mIcon = BitmapFactory.decodeFile(actualFullPath);
        } else {
            try {
                final ZipEntry fileEntry = kmzContainer.getEntry(href);
                InputStream stream = kmzContainer.getInputStream(fileEntry);
                mIcon = BitmapFactory.decodeStream(stream);
            } catch (Exception e) {
                mIcon = null;
            }
        }
    }

    public void setLatLonBox(double north, double south, double east, double west) {
        mCoordinates = new ArrayList<GeoPoint>(2);
        mCoordinates.add(new GeoPoint(north, west));
        mCoordinates.add(new GeoPoint(south, east));
    }

    /**
     * @return the corresponding GroundOverlay ready to display on the map
     */
    @Override
    public Overlay buildOverlay(MapView map, Style defaultStyle, Styler styler, KmlDocument kmlDocument) {
        Context context = map.getContext();
        GroundOverlay overlay = new GroundOverlay(context);
        if (mCoordinates.size() == 2) {
            GeoPoint pNW = mCoordinates.get(0);
            GeoPoint pSE = mCoordinates.get(1);
            overlay.setPosition(GeoPoint.fromCenterBetween(pNW, pSE));
            GeoPoint pNE = new GeoPoint(pNW.getLatitude(), pSE.getLongitude());
            int width = pNE.distanceTo(pNW);
            GeoPoint pSW = new GeoPoint(pSE.getLatitude(), pNW.getLongitude());
            int height = pSW.distanceTo(pNW);
            overlay.setDimensions((float) width, (float) height);
        }
        //TODO:
        //else if size=4, nonrectangular quadrilateral
        //else, error

        if (mIcon != null)
            overlay.setImage(new BitmapDrawable(mIcon));
        else {
            /* TODO: currently filling the canvas.
            ColorDrawable rect = new ColorDrawable(mColor);
      rect.setAlpha(255); //transparency will be applied below.
      overlay.setImage(rect);
      */
        }

        float transparency = 1.0f - Color.alpha(mColor) / 255.0f; //KML transparency is the transparency part of the "color" element.
        overlay.setTransparency(transparency);
        overlay.setBearing(-mRotation); //from KML counterclockwise to Google Maps API which is clockwise
        if (styler == null)
            overlay.setEnabled(mVisibility);
        else
            styler.onFeature(overlay, this);
        return overlay;
    }

    //Cloneable implementation ------------------------------------

    /**
     * write elements specific to GroundOverlay in KML format
     */
    @Override
    public void writeKMLSpecifics(Writer writer) {
        try {
            writer.write("<color>" + ColorStyle.colorAsKMLString(mColor) + "</color>\n");
            writer.write("<Icon><href>" + mIconHref + "</href></Icon>\n");
            writer.write("<LatLonBox>");
            GeoPoint pNW = mCoordinates.get(0);
            GeoPoint pSE = mCoordinates.get(1);
            writer.write("<north>" + pNW.getLatitude() + "</north>");
            writer.write("<south>" + pSE.getLatitude() + "</south>");
            writer.write("<east>" + pSE.getLongitude() + "</east>");
            writer.write("<west>" + pNW.getLongitude() + "</west>");
            writer.write("<rotation>" + mRotation + "</rotation>");
            writer.write("</LatLonBox>\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //Parcelable implementation ------------

    @Override
    public JsonObject asGeoJSON(boolean isRoot) {
        //TODO: GroundOverlay is not supported by GeoJSON. Output enclosing polygon with mColor?
        return null;
    }

    public KmlGroundOverlay clone() {
        KmlGroundOverlay kmlGroundOverlay = (KmlGroundOverlay) super.clone();
        kmlGroundOverlay.mCoordinates = KmlGeometry.cloneArrayOfGeoPoint(mCoordinates);
        return kmlGroundOverlay;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        super.writeToParcel(out, flags);
        out.writeString(mIconHref);
        out.writeParcelable(mIcon, flags);
        out.writeInt(mColor);
        out.writeFloat(mRotation);
        out.writeList(mCoordinates);
    }

}




Java Source Code List

com.ilm.sandwich.BackgroundService.java
com.ilm.sandwich.BuildConfig.java
com.ilm.sandwich.Config.java
com.ilm.sandwich.GoogleMapActivity.java
com.ilm.sandwich.Info.java
com.ilm.sandwich.MySupportMapFragment.java
com.ilm.sandwich.OsmMapActivity.java
com.ilm.sandwich.Settings.java
com.ilm.sandwich.StartChooser.java
com.ilm.sandwich.TouchableWrapper.java
com.ilm.sandwich.tools.Core.java
com.ilm.sandwich.tools.HttpRequests.java
com.ilm.sandwich.tools.Locationer.java
com.ilm.sandwich.tools.MapDownload.java
com.ilm.sandwich.tools.MyItemizedOverlay.java
com.ilm.sandwich.tools.PlacesAutoComplete.java
com.ilm.sandwich.tools.PlacesTextSearch.java
com.ilm.sandwich.tools.Statistics.java
com.ilm.sandwich.tools.SuggestionsAdapter.java
org.osmdroid.bonuspack.BuildConfig.java
org.osmdroid.bonuspack.BuildConfig.java
org.osmdroid.bonuspack.cachemanager.CacheManager.java
org.osmdroid.bonuspack.clustering.GridMarkerClusterer.java
org.osmdroid.bonuspack.clustering.MarkerClusterer.java
org.osmdroid.bonuspack.clustering.StaticCluster.java
org.osmdroid.bonuspack.kml.ColorStyle.java
org.osmdroid.bonuspack.kml.IconStyle.java
org.osmdroid.bonuspack.kml.KmlDocument.java
org.osmdroid.bonuspack.kml.KmlFeature.java
org.osmdroid.bonuspack.kml.KmlFolder.java
org.osmdroid.bonuspack.kml.KmlGeometry.java
org.osmdroid.bonuspack.kml.KmlGroundOverlay.java
org.osmdroid.bonuspack.kml.KmlLineString.java
org.osmdroid.bonuspack.kml.KmlMultiGeometry.java
org.osmdroid.bonuspack.kml.KmlPlacemark.java
org.osmdroid.bonuspack.kml.KmlPoint.java
org.osmdroid.bonuspack.kml.KmlPolygon.java
org.osmdroid.bonuspack.kml.LineStyle.java
org.osmdroid.bonuspack.kml.Style.java
org.osmdroid.bonuspack.location.FlickrPOIProvider.java
org.osmdroid.bonuspack.location.GeoNamesPOIProvider.java
org.osmdroid.bonuspack.location.GeocoderGisgraphy.java
org.osmdroid.bonuspack.location.GeocoderNominatimOld.java
org.osmdroid.bonuspack.location.GeocoderNominatim.java
org.osmdroid.bonuspack.location.NominatimPOIProvider.java
org.osmdroid.bonuspack.location.POI.java
org.osmdroid.bonuspack.location.PicasaPOIProvider.java
org.osmdroid.bonuspack.mapsforge.GenericMapView.java
org.osmdroid.bonuspack.mapsforge.MapsForgeTileModuleProvider.java
org.osmdroid.bonuspack.mapsforge.MapsForgeTileProvider.java
org.osmdroid.bonuspack.mapsforge.MapsForgeTileSource.java
org.osmdroid.bonuspack.overlays.DefaultInfoWindow.java
org.osmdroid.bonuspack.overlays.ExtendedOverlayItem.java
org.osmdroid.bonuspack.overlays.FolderOverlay.java
org.osmdroid.bonuspack.overlays.GroundOverlay.java
org.osmdroid.bonuspack.overlays.InfoWindow.java
org.osmdroid.bonuspack.overlays.ItemizedOverlayWithBubble.java
org.osmdroid.bonuspack.overlays.MapEventsOverlay.java
org.osmdroid.bonuspack.overlays.MapEventsReceiver.java
org.osmdroid.bonuspack.overlays.MarkerInfoWindow.java
org.osmdroid.bonuspack.overlays.Marker.java
org.osmdroid.bonuspack.overlays.Polygon.java
org.osmdroid.bonuspack.overlays.Polyline.java
org.osmdroid.bonuspack.routing.GoogleRoadManager.java
org.osmdroid.bonuspack.routing.MapQuestRoadManager.java
org.osmdroid.bonuspack.routing.OSRMRoadManager.java
org.osmdroid.bonuspack.routing.RoadLeg.java
org.osmdroid.bonuspack.routing.RoadManager.java
org.osmdroid.bonuspack.routing.RoadNode.java
org.osmdroid.bonuspack.routing.Road.java
org.osmdroid.bonuspack.utils.BonusPackHelper.java
org.osmdroid.bonuspack.utils.DouglasPeuckerReducer.java
org.osmdroid.bonuspack.utils.HttpConnection.java
org.osmdroid.bonuspack.utils.PolylineEncoder.java
org.osmdroid.bonuspack.utils.WebImageCache.java