MyGoogleMaps.java :  » App » dostufftogether » com » appspot » android2gather » location » Android Open Source

Android Open Source » App » dostufftogether 
dostufftogether » com » appspot » android2gather » location » MyGoogleMaps.java
/*
   This file is part of the 'Let's Do Stuff Together' project
   http://code.google.com/p/dostufftogether/

   Copyright 2010 Christoph Fuchs, Stefan Thaler

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   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.appspot.android2gather.location;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.appspot.android2gather.R;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * Google Maps MapActivity implementation. Uses an overlay to choose a meeting
 * point. Uses GeoLocation service to map addresses to geoPoints and vice versa.
 * 
 * @author Christoph 'Gigi' Fuchs
 * 
 */
public class MyGoogleMaps extends MapActivity {

  /** 
   * Constant to identify the longitude of a GeoPoint
   */
  public static final String GEO_LONGITUDE_E6 = "longitude";
  /**
   * Constant to identify the latitude of a GeoPoint
   */
  public static final String GEO_LATITUDE_E6 = "latitude";
  /**
   * Constant to identify the address (name) of a GeoPoint
   */
  public static final String GEO_ADDRESS = "address";
  
  /*
   * Private fields
   */
  private List<Overlay> mapOverlays;
  private Drawable drawable;
  private ClickableItemizedOverlay itemizedOverlay;
  private MapView mapView;
  private Button okButton;
  
  /* (non-Javadoc)
   * @see com.google.android.maps.MapActivity#isRouteDisplayed()
   */
  @Override
  protected boolean isRouteDisplayed() {
    return false;
  }
  
  /* (non-Javadoc)
   * @see com.google.android.maps.MapActivity#onCreate(android.os.Bundle)
   */
  @Override
  protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.map);
    
    
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapView.setStreetView(true);
    mapOverlays = mapView.getOverlays();
    drawable = this.getResources().getDrawable(R.drawable.marker);
    itemizedOverlay = new ClickableItemizedOverlay(drawable);
    mapOverlays.add(itemizedOverlay);
    
    okButton = (Button) findViewById(R.id.button_mapOk);
    okButton.setOnClickListener(new OnClickListener() {
      
      @Override
      public void onClick(View v) {
        ArrayList<OverlayItem> overlays = itemizedOverlay.getOverlays();
              
        int latitudeE6 = overlays.get(0).getPoint().getLatitudeE6();
        int longitudeE6 = overlays.get(0).getPoint().getLongitudeE6();
        
        /*
         * Hand back the result (long, lat)
         */
        Intent resultIntent = new Intent();
        resultIntent.putExtra(GEO_LONGITUDE_E6, longitudeE6);
        resultIntent.putExtra(GEO_LATITUDE_E6, latitudeE6);
        resultIntent.putExtra(GEO_ADDRESS, getAddressNameFromGeoCoder(mapView.getContext(), latitudeE6 / 1E6, longitudeE6 / 1E6));
        setResult(Activity.RESULT_OK, resultIntent);
        finish();
      }

    });
  }
  
  /**
   * 
   * TODO Describe the purpose of this method.
   * @param context 
   * @param latitudeE6
   * @param longitudeE6
   * @return
   */
  public static String getAddressNameFromGeoCoder(Context context, double latitudeE6,
      double longitudeE6) {
    Geocoder geoCoder = new Geocoder(context);
    try {
      List<Address> addresses = geoCoder.getFromLocation(latitudeE6,
          longitudeE6, 1);

      String add = "";
      if (addresses.size() > 0) {
        for (int i = 0; i < addresses.get(0).getMaxAddressLineIndex(); i++)
          add += addresses.get(0).getAddressLine(i) + "\n";
      }

      Log.v("Map", "GeoCoder returned address " + add);
      return add;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "Address not found.";
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.