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 URI encoded strings 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.

 
// Create a Uri from an intent string. Use the result to create an Intent.
Uri gmmIntentUri = Uri.parse("google.streetview:cbll=46.414382,10.013988"); // Create an Intent from gmmIntentUri. Set the action to ACTION_VIEW
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
// Make the Intent explicit by setting the Google Maps package
mapIntent.setPackage("com.google.android.apps.maps"); // Attempt to start an activity that can handle the Intent
startActivity(mapIntent);

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.

 
if (mapIntent.resolveActivity(getPackageManager()) != null) {
    ...
}

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

 
Uri gmmIntentUri = Uri.parse("geo:37.7749,-122.4194");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(mapIntent);
}

URI 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:

 
Uri gmmIntentUri = Uri.parse("geo:37.7749,-122.4192?q=" + Uri.encode("1st & Pike, Seattle"));

Display a map

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

 
geo:latitude,longitude?z=zoom

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

 
// Creates an Intent that will load a map of San Francisco
Uri gmmIntentUri = Uri.parse("geo:37.7749,-122.4194");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

Search 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.

 
geo:latitude,longitude?q=query
geo:0,0?q=my+street+address
geo:0,0?q=latitude,longitude(label)

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-escaped, 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.

Categorical search

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:

 
// Search for restaurants nearby
Uri gmmIntentUri = Uri.parse("geo:0,0?q=restaurants");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
 
// Search for restaurants in San Francisco
Uri gmmIntentUri = Uri.parse("geo:37.7749,-122.4194?q=restaurants");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

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.

 
Uri gmmIntentUri = Uri.parse("geo:37.7749,-122.4194?z=10&q=restaurants");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

Location search

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

 
Uri gmmIntentUri = Uri.parse("geo:0,0?q=1600 Amphitheatre Parkway, Mountain+View, California");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

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.

 
// Searching for 'Main Street' will return too many results
Uri gmmIntentUri = Uri.parse("geo:0,0?q=101+main+street");

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

 
// Searches for 'Main Street' near San Francisco
Uri gmmIntentUri = Uri.parse("geo:37.7749,-122.4194?q=101+main+street");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

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.

 
// Display a label at the location of Google's Sydney office
Uri gmmIntentUri = Uri.parse("geo:0,0?q=-33.8666,151.1957(Google+Sydney)");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

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

 
// Display the location of Google, San Francisco using a global plus code.
Uri gmmIntentUri = Uri.parse("http://plus.codes/849VQJQ5+XX");
// Equivalently, define the same location using a local plus code
gmmIntentUri = Uri.parse("https://plus.codes/QJQ5+XX,San%20Francisco");
// Construct and use the Intent as in the examples above

Launch 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 users current location.

 
google.navigation:q=a+street+address
google.navigation:q=latitude,longitude

Parameters

  • q: Sets the end point for navigation searches. This can be a latitude,longitude 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, defaults to driving, and can be set to one of:

    • d for driving
    • w for walking
    • b for bicycling
  • avoid sets features the route should try to avoid. Avoid is optional and can be set to one or more of:

    • t for tolls
    • h for highways
    • f for ferries

Examples

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

 
Uri gmmIntentUri = Uri.parse("google.navigation:q=Taronga+Zoo,+Sydney+Australia");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

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

 
Uri gmmIntentUri = Uri.parse("google.navigation:q=Taronga+Zoo,+Sydney+Australia&avoid=tf");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

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

 
Uri gmmIntentUri = Uri.parse("google.navigation:q=Taronga+Zoo,+Sydney+Australia&mode=b");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

Display 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 areaUser contributed Photospheres, and Street View special collectionsare also available.

 
google.streetview:cbll=latitude,longitude&cbp=0,bearing,0,zoom,tilt
google.streetview:panoid=id&cbp=0,bearing,0,zoom,tilt

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.

 
// Displays an image of the Swiss Alps
Uri gmmIntentUri = Uri.parse("google.streetview:cbll=46.414382,10.013988");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
 
// Uses a PanoID to show an image from Maroubra beach in Sydney, Australia
Uri gmmIntentUri = Uri.parse("google.streetview:panoid=Iaa2JyfIggYAAAQfCZU9KQ");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
 
// Opens Street View between two Pyramids in Giza. The values passed to the
// cbp parameter will angle the camera slightly up, and towards the east.
Uri gmmIntentUri = Uri.parse("google.streetview:cbll=29.9774614,31.1329645&cbp=0,30,0,0,-15");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

google地图的url参数的更多相关文章

  1. Google 地图切片URL地址解析

    一.Google地图切片的投影方式及瓦片索引机制 1.地图投影 Google地图采用的是Web墨卡托投影(如下图),为了方便忽略了两极变形较大的地区,把世界地图做成了一个边长等于赤道周长的正方形(赤道 ...

  2. JavaScript一个google地图获取

    <script type="text/javascript"> /** * 返回一个新创建的<img>元素,该元素用于在获取到地理位置信息后,显示一张Goo ...

  3. 如何使用google地图的api(整理)

    如何使用google地图的api(整理) 一.总结 一句话总结:直接用script标签引google地图api即可. 1.如何使用google地图的api? 页面引用javascript文件<s ...

  4. Blazor组件自做五 : 使用JS隔离封装Google地图

    Blazor组件自做五: 使用JS隔离封装Google地图 运行截图 演示地址 正式开始 1. 谷歌地图API 谷歌开发文档 开始学习 Maps JavaScript API 的最简单方法是查看一个简 ...

  5. Google地图开发总结

    我们经常使用地图查位置.看公交.看街景,同时地图还开放第三方的API给开发者.利用这些API进行地图的个性化的展示和控制,例如北京被水淹了,开发一个网页显示北京被淹的地图,地图上面标志被水淹的位置.严 ...

  6. Google 地图 API V3 使用入门

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  7. Google 地图 API V3 针对移动设备进行开发

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  8. Google 地图 API V3 之事件

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  9. Google 地图 API V3 之 叠加层

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

随机推荐

  1. hao360恶意篡改IE首页——修复方法

    设置浏览器首页空白或自定义后,点击开始菜单,找到IE浏览器,右键进入属性,找到shortcut里面“目标”,你会看到里面链接到的是hao360什么乱糟糟的,这才是以上问题的关键原因.删除图1中红色内容 ...

  2. sessionStorage & string typeof

    sessionStorage & string typeof

  3. 微信小程序填坑之旅一(接入)

    一.小程序简介 小程序是什么? 首先“程序”这两个字我们不陌生.看看你手机上的各个软件,那就是程序.平时的程序是直接跑在我们原生的操作系统上面的.小程序是间接跑在原生系统上的.因为它嵌入在微信中,受微 ...

  4. 【BZOJ1965】[AHOI2005]洗牌(数论)

    [BZOJ1965][AHOI2005]洗牌(数论) 题面 BZOJ 洛谷 题解 考虑反过来做这个洗牌的操作,假定当前牌是第\(l\)张. 因为之前洗的时候考虑了前一半和后一半,所以根据\(l\)的奇 ...

  5. bzoj4458 GTY的OJ (优先队列+倍增)

    把超级钢琴放到了树上. 这次不用主席树了..本来以为会好写一点没想到细节更多(其实是树上细节多) 为了方便,对每个点把他的那个L,R区间转化成两个深度a,b,表示从[a,b)选一个最小的前缀和(到根的 ...

  6. (转)Java随机数

    1 随机数的三种产生方式 本章先讲解Java随机数的几种产生方式,然后通过示例对其进行演示. 广义上讲,Java中的随机数的有三种产生方式: (01). 通过System.currentTimeMil ...

  7. 简单的sql分组统计

    一个记录员工打卡时间的表,只有两个有效字段 员工名称,打卡时间,现在要统计某一天中,每个员工的打卡次数.最早打卡时间.最晚打卡时间,问sql怎么写? 其实这个sql很简单, 1.首先要明确既然是按每个 ...

  8. 写一个Windows服务

    做了两个和Windows服务有关的项目了,最开始的时候没做过,不懂,现在明白了许多.需要注意的是,如果不想登录什么的,最后在添加安装程序的那里选择那个字长的右键属性,把启动方式改为local syst ...

  9. matplotlib交互模式与pacharm单独Figure设置

    matplotlib交互模式与pacharm单独Figure设置 觉得有用的话,欢迎一起讨论相互学习~Follow Me Matpotlib交互模式 在运行python程序时有时候需要生成以下的 动态 ...

  10. Maven的国内镜像(解决jar下载过慢)

    Maven简介 maven作为一个项目管理工具确实非常好用,结果在使用时候,你会发现下载jar速度不如自己在网上下载.之前oschina的中央仓库可用,现在oschina的maven服务器关了,只能拿 ...