A map ID is a unique identifier that's used to represent a single instance of a Google Map. You use map IDs to enable features or manage or style maps on your websites and in your applications. You can create map IDs for each platform you need--JavaScript, Android, iOS, or Static maps--in your Google Cloud console project on the Map Management page.
For more details and features that use map IDs, see Map ID overview.
Required permissions
To create or manage any map IDs in your project, you must use a principal with the appropriate role-level permissions, Editor or Owner, on the Cloud console IAM page for the project. For details, see IAM basic and predefined roles reference.
Create map IDs
Create map IDs in the Cloud console following these steps:
Sign in to and open a Cloud console project with the required permissions.
In the Cloud console, go to the Maps Management page.
Click Create map ID.
On the Create new map ID page, do the following:
- For Name, give the map ID a name.
- Optional: For Description, describe what the map ID is used for.
- For Map type, select the platform on which you plan to use the map ID. If you choose JavaScript, also choose a Raster (the default) or Vector map type. For more information on vector maps, see Vector Maps.
- Click Save to show your new map ID.
Associate a map ID to a map style
If you are using cloud-based maps styling, you associate a map style with your map ID. For details, see Associate a.
Add the map ID to your app
Android
Add your map ID through a <fragment>
element in the
activity's layout file, by using the MapView
class, or programmatically
using the GoogleMapOptions
class.
For example, assume you created a map ID that is stored as a
string value named map_id
in res/values/strings.xml
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="map_id">MAP_ID</string>
</resources>
For maps added through a <fragment>
element in the activity's layout file,
all map fragments that should have the custom style must specify the
map ID in the map:mapId
attribute:
<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
map:name="com.google.android.gms.maps.SupportMapFragment"
…
map:mapId="@string/map_id" />
You can also use the map:mapId
attribute of the MapView
class to specify
a map ID:
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
....
map:mapId="@string/map_id" />
To specify a map ID programmatically, pass it to a
MapFragment
instance using the GoogleMapOptions
class:
Java
MapFragment mapFragment = MapFragment.newInstance(
new GoogleMapOptions()
.mapId(getResources().getString(R.string.map_id)));
Kotlin
val mapFragment = MapFragment.newInstance(
GoogleMapOptions()
.mapId(resources.getString(R.string.map_id))
)
In Android Studio, build and run your app as you normally would. Custom styles configured in the first step are applied to all maps with the specified map ID.
iOS
To instantiate a map using a map ID, do the folowing:
- Create a
GMSMapID
with the map ID string from Cloud console. - Create a
GMSMapView
specifying the map ID you just created.
Swift
let camera = GMSCameraPosition(latitude: 47.0169, longitude: -122.336471, zoom: 12)
let mapID = GMSMapID(identifier: "MAP_ID")
let mapView = GMSMapView(frame: .zero, mapID: mapID, camera: camera)
self.view = mapView
Objective-C
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:47.0169
longitude:-122.336471
zoom:12];
GMSMapID *mapID = [GMSMapID mapIDWithIdentifier:@"MAP_ID"];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero mapID:mapID camera:camera];
self.view = mapView;
If you are using your own map ID, you can set your map ID in the Cloud console to have a new style at any time, and that style will be reflected on your map view automatically for you and users within about six hours.
If you want to see the changes immediately, you can close out and restart your app by exiting the app, forcing a quit of the app from the recently-used apps list, and then reopening it. The updated map will then be visible.
JavaScript
To create a map with a map ID in your application code:
If you are already customizing your map with embedded JSON code, remove the
styles
property from yourMapOptions
object; otherwise, skip this step.Add a map ID to the map using the
mapId
property. For example:
map = new google.maps.Map(document.getElementById('map'), { center: {lat: -34.397, lng: 150.644}, zoom: 8, mapId: 'MAP_ID' });
Maps Static
To add a map ID to a new or existing map that uses one of our
web-service APIs, append the map_id
URL parameter and set it to your
map ID. This example shows adding a map ID to
a map using Maps Static API.
<img src="https://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=600x300&maptype=roadmap&markers=color:blue%7Clabel:S%7C40.702147,-74.015794&markers=color:green%7Clabel:G%7C40.711614,-74.012318&markers=color:red%7Clabel:C%7C40.718217,-73.998284&key=YOUR_API_KEY&map_id=MAP_ID&signature=YOUR_SIGNATURE" />