Android intent map route

Bramengton / NavigationApps.java

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

import android.content.ActivityNotFoundException ;
import android.content.Context ;
import android.content.ContextWrapper ;
import android.content.Intent ;
import android.content.pm.PackageManager ;
import android.net.Uri ;
import android.util.Log ;
import org.json.JSONArray ;
import org.json.JSONException ;
/**
* @author Bramengton on 16.08.2018.
*/
public final class NavigationApps extends ContextWrapper <
private Intent mIntent;
private static final Intent GOOGLE = new Intent ( Intent . ACTION_VIEW )
.setPackage( » com.google.android.apps.maps » );
private static final Intent WAZE = new Intent ( Intent . ACTION_VIEW )
.setPackage( » com.waze » );
private static final Intent MAPSME = new Intent ( » com.mapswithme.maps.pro.action.BUILD_ROUTE » )
.setPackage( » com.mapswithme.maps.pro » );
private static final Intent NAVITEL = new Intent ( Intent . ACTION_VIEW )
.setPackage( » com.navitel » );
private static final Intent CITYGUIDE = new Intent ( Intent . ACTION_SEND )
.setType( » vnd.android.cursor.item/vnd.net.probki.cityguide.cmd » )
.setPackage( » cityguide.probki.net » );
private static final Intent OSMAND = new Intent ( Intent . ACTION_VIEW )
.setPackage( » net.osmand » );
private static final Intent YANDEX = new Intent ( » ru.yandex.yandexnavi.action.BUILD_ROUTE_ON_MAP » )
.setPackage( » ru.yandex.yandexnavi » );
private static final Intent TWOGIS = new Intent ( Intent . ACTION_VIEW )
.setPackage( » ru.dublgis.dgismobile » );
public enum Apps <
GOOGLE , WAZE , OSMAND , MAPSME , NAVITEL , CITYGUIDE , YANDEX , TWOGIS
>
// App link to Google Play Market
private static final String MARKET_LINK = » market://details?id=%s » ;
// Google api for delivery to navigation app next point coordinates
private static final String GOOGLE_NAVIGATION = » google.navigation:ll=%s,%s » ;
private static final String GOOGLE_MAP_NAVIGATION = » http://maps.google.com/maps?%s » ; // saddr=%1$s&daddr=%2$s+to:%3$s
// Waze api for delivery to weze next point coordinates
private static final String WAZE_NAVIGATION = » waze://?ll=%s,%s&navigate=yes » ;
private static final String CITYGUIDE_NAVIGATION = » cgcmd delroute setroute %s » ;
// 2GIS api for delivery to 2gis navigator next point coordinates in custom format /from/ , /to/ ,
// — vehicle type (car (car route), ctx (public transport), pedestrian (hiking route), taxi) default is «car»
// , — coordinates of the beginning and end of the route.
private static final String TWOGIS_NAVIGATION = » dgis://2gis.ru/routeSearch/rsType/car/to/%s,%s » ;
private static final String TWOGIS_MAP_NAVIGATION = » dgis://2gis.ru/routeSearch/rsType/car/%s » ;
public NavigationApps ( Context base ) <
super (base);
>
/**
* Set geo coordinates point of destination point
* @param flag select one of enumerated apps
* @param points array of coordinates like (lat, lon, lat, lon)
*/
public NavigationApps setDestination ( final Apps flag , LatLng . points ) <
mIntent = new Intent ();
switch (flag) <
case GOOGLE :
if (points . length == 1 ) <
mIntent = GOOGLE . setData( Uri . parse( String . format( GOOGLE_NAVIGATION , points[ 0 ] . latitude, points[ 0 ] . longitude)));
break ;
>
StringBuilder google = new StringBuilder ();
int i = 0 ;
for ( LatLng point : points) <
switch (i) <
case 0 :
google . append( » &daddr= » ) . append(point . toString());
break ;
default :
google . append( » +to: » ) . append(point . toString());
break ;
>
i ++ ;
>
mIntent = GOOGLE . setData( Uri . parse( String . format( GOOGLE_MAP_NAVIGATION , google . toString())));
break ;
case NAVITEL :
mIntent = NAVITEL . setData( Uri . parse( String . format( GOOGLE_NAVIGATION , points[ 0 ] . latitude, points[ 0 ] . longitude)));
break ;
case OSMAND :
mIntent = OSMAND . setData( Uri . parse( String . format( GOOGLE_NAVIGATION , points[ 0 ] . latitude, points[ 0 ] . longitude)));
break ;
case WAZE :
mIntent = WAZE . setData( Uri . parse( String . format( WAZE_NAVIGATION , points[ 0 ] . latitude, points[ 0 ] . longitude)));
break ;
case MAPSME :
mIntent = MAPSME ;
mIntent . putExtra( » lat_to » , points[ 0 ] . latitude);
mIntent . putExtra( » lon_to » , points[ 0 ] . longitude);
mIntent . putExtra( » router » , » vehicle » );
break ;
case YANDEX :
mIntent = YANDEX ;
mIntent . putExtra( » lat_to » , points[ 0 ] . latitude);
mIntent . putExtra( » lon_to » , points[ 0 ] . longitude);
break ;
case CITYGUIDE :
mIntent = CITYGUIDE ;
StringBuilder cityguide = new StringBuilder ();
// cityguide.append(points.length > 1 ? points.length/2 : 0);
cityguide . append(points . length > 1 ? points . length : 0 );
for ( LatLng point : points) <
cityguide . append( » » ) . append(point . latitude) . append( » » ) . append(point . longitude);
>
// Log.e(«LOGIN», String.format(«CITYGUIDE: cgcmd delroute setroute %s», cityguide.toString()));
mIntent . putExtra( Intent . EXTRA_TEXT , String . format( CITYGUIDE_NAVIGATION , cityguide . toString()));
break ;
case TWOGIS :
if (points . length == 1 ) <
mIntent = TWOGIS . setData( Uri . parse( String . format( TWOGIS_NAVIGATION , points[ 0 ] . longitude, points[ 0 ] . latitude)));
break ;
>
StringBuilder tgis = new StringBuilder ();
int j = 0 ;
for ( LatLng point : points) <
if (j == 0 ) tgis . append( String . format( » from/%s,%s » , point . longitude, point . latitude));
else <
if (j > 1 ) break ; // only for 2 points
tgis . append( String . format( » /to/%s,%s » , point . longitude, point . latitude));
>
j ++ ;
>
mIntent = TWOGIS . setData( Uri . parse( String . format( TWOGIS_MAP_NAVIGATION , tgis . toString())));
break ;
>
return this ;
>
/**
* Check selected navigation application is installed or not
* @param flag select one of enumerated apps
*/
public void checkPackage ( Apps flag ) throws ActivityNotFoundException <
Intent intent;
switch (flag) <
case GOOGLE :
intent = GOOGLE ;
break ;
case NAVITEL :
intent = NAVITEL ;
break ;
case OSMAND :
intent = OSMAND ;
break ;
case WAZE :
intent = WAZE ;
break ;
case MAPSME :
intent = MAPSME ;
break ;
case YANDEX :
intent = YANDEX ;
break ;
case CITYGUIDE :
intent = CITYGUIDE ;
break ;
case TWOGIS :
intent = TWOGIS ;
break ;
default : return ;
>
if ( ! isPackageInstalled(intent)) <
openMarket(intent);
>
>
/**
* Show the selected navigation application and build a route
* @param install if navigation application not installed open GooglePlay on instalation page
*/
public void guideMe ( boolean install ) throws ActivityNotFoundException <
if (isPackageInstalled(mIntent)) <
startActivity(mIntent . addFlags( Intent . FLAG_ACTIVITY_NEW_TASK )
.addFlags( Intent . FLAG_ACTIVITY_RESET_TASK_IF_NEEDED )
.addFlags( Intent . FLAG_ACTIVITY_CLEAR_TOP ));
> else <
if (install) <
openMarket(mIntent);
> else <
throw new ActivityNotFoundException ();
>
>
>
private boolean isPackageInstalled ( Intent intent ) <
try <
return getApplicationContext() . getPackageManager() . getPackageInfo(intent . getPackage(), 0 ) . packageName . equalsIgnoreCase(intent . getPackage());
> catch ( PackageManager . NameNotFoundException e) <
return false ;
>
>
private void openMarket ( Intent intent ) <
startActivity(
new Intent ( Intent . ACTION_VIEW , Uri . parse( String . format( MARKET_LINK , intent . getPackage())))
.addFlags( Intent . FLAG_ACTIVITY_NEW_TASK )
.addFlags( Intent . FLAG_ACTIVITY_RESET_TASK_IF_NEEDED )
.addFlags( Intent . FLAG_ACTIVITY_CLEAR_TOP )
);
>
public static class LatLng <
public double latitude;
public double longitude;
public LatLng ( double var1 , double var3 ) <
if ( — 180.0D var3 && var3 180.0D ) <
this . longitude = var3;
> else <
this . longitude = ((var3 — 180.0D ) % 360.0D + 360.0D ) % 360.0D — 180.0D ;
>
this . latitude = Math . max( — 90.0D , Math . min( 90.0D , var1));
>
public final int hashCode () <
long var2 = Double . doubleToLongBits( this . latitude);
int var1 = 31 + ( int )(var2 ^ var2 >>> 32 );
var2 = Double . doubleToLongBits( this . longitude);
return var1 * 31 + ( int )(var2 ^ var2 >>> 32 );
>
public final boolean equals ( Object var1 ) <
if ( this == var1) <
return true ;
> else if ( ! (var1 instanceof LatLng )) <
return false ;
> else <
LatLng var2 = ( LatLng )var1;
return Double . doubleToLongBits( this . latitude) == Double . doubleToLongBits(var2 . latitude) && Double . doubleToLongBits( this . longitude) == Double . doubleToLongBits(var2 . longitude);
>
>
public final String toString () <
double var1 = this . latitude;
double var3 = this . longitude;
return String . format( » %s, %s » ,var1,var3);
>
>
>
Читайте также:  Суперюзер для андроид что это такое

You can’t perform that action at this time.

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.

Источник

Google Maps Intents for Android

The Google Maps app for Android exposes several intents that you can use to launch Google Maps in display, search, navigation, or Street View modes. If you want to embed a map in your app, please refer to the Google Maps Android API Getting Started Guide.

Note: Maps URLs let you build a universal, cross-platform URL to launch Google Maps and perform searches, get directions, display map views, and display panoramic images. It is recommended that you use the cross-platform Maps URLs to launch Google Maps, since these universal URLs allow for broader handling of the maps requests no matter which platform the user is on. You should only use the Android-specific Maps Intents for features that may only be functional on a mobile platform (for example, turn-by-turn navigation).

Overview

Intents let you start an activity in another app by describing a simple action you’d like to perform (such as «display a map» or «show directions to the airport») in an Intent object. The Google Maps app for Android supports several different intents, allowing you to launch the Google Maps app and perform one of four actions:

  1. Display a map at a specified location and zoom level.
  2. Search for locations or places, and display them on a map.
  3. Request directions from one location to another. Directions can be returned for three modes of transportation: driving, walking, bicycling.
  4. Display panorama imagery in Google Street View.

This page describes the intents that you can use with Google Maps app for Android. For more information on Intents and Intent Filters, or Intents common to the Android platform, refer to the Android developer documentation.

Intent requests

In order to launch Google Maps with an intent you must first create an Intent object, specifying its action, URI and package.

  • Action: All Google Maps intents are called as a View action — ACTION_VIEW .
  • URI: Google Maps intents use URL encoded that specify a desired action, along with some data with which to perform the action.
  • Package: Calling setPackage(«com.google.android.apps.maps») will ensure that the Google Maps app for Android handles the Intent. If the package isn’t set, the system will determine which apps can handle the Intent . If multiple apps are available, the user may be asked which app they would like to use.
Читайте также:  Прошить андроид с линукс

After creating the Intent , you can request that the system launch the related app in a number of ways. A common method is to pass the Intent to the startActivity() method. The system will launch the necessary app — in this case Google Maps — and start the corresponding Activity .

Kotlin

If the system cannot identify an app that can respond to the intent, your app may crash. For this reason, you should first verify that a receiving application is installed before you present one of these intents to a user.

To verify that an app is available to receive the intent, call resolveActivity() on your Intent object. If the result is non-null, there is at least one app that can handle the intent and it’s safe to call startActivity() . If the result is null , you should not use the intent and, if possible, you should disable the feature that invokes the intent.

Kotlin

For example, to display a map of San Francisco, you can use the following code:

Kotlin

URL encoded query strings

All strings passed to the Google Maps Intents must be URI encoded. For example, the string «1st & Pike, Seattle» should become 1st%20%26%20Pike%2C%20Seattle . Spaces in the string can be encoded with %20 or replaced with the plus sign (+).

You can use the android.net.Uri parse() method to encode your strings. For example:

Kotlin

Displaying a map

Use the geo: intent to display a map at a specified location and zoom level.

Parameters

  • latitude and longitude set the center point of the map.
  • z optionally sets the initial zoom level of the map. Accepted values range from 0 (the whole world) to 21 (individual buildings). The upper limit can vary depending on the map data available at the selected location.

Examples

Kotlin

Searching for a location

Use this intent to display search queries within a specified viewport. When the query has a single result, you can use this intent to display a pin at a particular place or address, such as a landmark, business, geographic feature, or town.

Parameters

In addition to the parameters used to display a map, Search supports the following parameters:

q defines the place(s) to highlight on the map. The q parameter is required for all Search requests. It accepts a location as either a place name or address. The string should be URL-encoded, so an address such as «City Hall, New York, NY» should be converted to City+Hall,New+York,NY.

label lets you set a custom label at a place identified on the map. The label must be specified as a String.

If you pass a general search term, Google Maps will attempt to find a location near the lat/lng you specified that matches your criteria. If no location is specified, Google Maps will try to find nearby listings. For example:

Kotlin

You can further bias the search results by specifying a zoom parameter along with the query string. In the below example, adding a zoom of 10 will attempt to find restaurants at a city level instead of nearby.

Kotlin

Searching for a specific address will display a pin at that location.

Kotlin

The above example sets a lat/lng of 0 , 0 , but passes an address as a query string. When searching for a very specific location, the latitude and longitude are not required. However, if you do not know the exact address, you can attempt to bias the results of the search by specifying a coordinate. For example, performing an address search for ‘Main Street’ will return too many results.

Kotlin

Adding a lat/lng to the intent URI will bias the results towards a particular area:

Читайте также:  Разрешения для ярлыков андроид

Kotlin

When you know your search will return a single value, you may wish to pass an optional label. Labels must be specified as a String, and will appear under the map marker. Note that labels are only available when q is specified as a lat/lng coordinate.

Kotlin

As an alternative to a street address or a latitude/longitude, you can display a pin at a known location using a plus code.

Kotlin

Launching turn-by-turn navigation

Use this intent to launch Google Maps navigation with turn-by-turn directions to the address or coordinate specified. Directions are always given from the user’s current location.

Parameters

q : Sets the end point for navigation searches. This value can be latitude, longitude coordinates or a query formatted address. If it is a query string that returns more than one result, the first result will be selected.

mode sets the method of transportation. Mode is optional, and can be set to one of:

  • d for driving (default)
  • b for bicycling
  • l for two-wheeler
  • w for walking

avoid sets features the route should try to avoid. Avoid is optional and can be set to one or more of:

Examples

The below Intent will request turn-by-turn navigation to Taronga Zoo, in Sydney Australia:

Kotlin

If you prefer not to pay tolls or ride a ferry, you can request routing that tries to avoid those things.

Kotlin

If you’d prefer a bit of exercise, you can request bicycling directions instead.

Kotlin

If you’d prefer taking a motorized two-wheeler, you can request that the directions include narrow roads and trails unavailable to cars. The below intent returns a route in India.

Kotlin

Displaying a Street View panorama

Use the google.streetview intent to launch Google Street View. Google Street View provides panoramic views from designated locations throughout its coverage area. User contributed Photospheres, and Street View special collections are also available.

Parameters

All google.streetview URIs must include either a cbll or a panoid parameter.

cbll accepts a latitude and a longitude as comma-separated values ( 46.414382,10.013988 ). The app will display the panorama photographed closest to this location. Because Street View imagery is periodically refreshed, and photographs may be taken from slightly different positions each time, it’s possible that your location may snap to a different panorama when imagery is updated.

panoid is a specific panorama ID. Google Maps will use the panorama ID if both a panoid and a cbll are specified. Panorama IDs are available to an Android app from the StreetViewPanoramaLocation object.

cbp is an optional parameter that adjusts the initial orientation of the camera. The cbp parameter takes 5 comma-separated values, all of which are optional. The most significant values are the second, fourth and fifth which set the bearing, zoom and tilt respectively. The first and third values are not supported, and should be set to 0 .

  • bearing : indicates the compass heading of the camera in degrees clockwise from North. True north is 0, east is 90, south is 180, west is 270. Values passed to bearing will wrap; that is, 0°, 360° and 720° all point in the same direction. Bearing is defined as the second of five comma-separated values.
  • zoom : Sets the zoom level of the camera. The default zoom level is set at 0. A zoom of 1 would double the magnification. The zoom is clamped between 0 and the maximum zoom level for the current panorama. This means that any value falling outside this range will be set to the closest extreme that falls within the range. For example, a value of -1 will be set to 0. Zoom is the fourth of five comma-separated values.
  • tilt : specifies the angle, up or down, of the camera. The range is -90 through 0 to 90, with 90 looking straight down, 0 centered on the horizon, and -90 looking straight up.

Examples

Below are some examples of using the Street View intent.

Kotlin

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Источник

Оцените статью