show On Google Map - Android Map

Android examples for Map:Google Map

Description

show On Google Map

Demo Code

/*/*from ww w .ja  va  2  s .  c  o  m*/
 * Copyright (C) 2009 The Android Open Source Project
 *
 * 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.
 */
import java.util.Locale;

import com.android.camera.CameraActivity;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

public class Main {

  private static final String TAG = "Util";
  private static final String MAPS_CLASS_NAME = "com.google.android.maps.MapsActivity";
  private static final String MAPS_PACKAGE_NAME = "com.google.android.apps.maps";

  /**
   * Starts GMM with the given location shown. If this fails, and GMM could not be
   * found, we use a geo intent as a fallback.
   *
   * @param activity
   *          the activity to use for launching the Maps intent.
   * @param latLong
   *          a 2-element array containing {latitude/longitude}.
   */
  public static void showOnMap(Activity activity, double[] latLong) {
    try {
      // We don't use "geo:latitude,longitude" because it only centers
      // the MapView to the specified location, but we need a marker
      // for further operations (routing to/from).
      // The q=(lat, lng) syntax is suggested by geo-team.
      String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?f=q&q=(%f,%f)", latLong[0], latLong[1]);
      ComponentName compName = new ComponentName(MAPS_PACKAGE_NAME, MAPS_CLASS_NAME);
      Intent mapsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)).setComponent(compName);
      activity.startActivityForResult(mapsIntent, CameraActivity.REQ_CODE_DONT_SWITCH_TO_PREVIEW);
    } catch (ActivityNotFoundException e) {
      // Use the "geo intent" if no GMM is installed
      Log.e(TAG, "GMM activity not found!", e);
      String url = String.format(Locale.ENGLISH, "geo:%f,%f", latLong[0], latLong[1]);
      Intent mapsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
      activity.startActivity(mapsIntent);
    }
  }

}

Related Tutorials