Skip to content

Commit 8caba9c

Browse files
authored
feat: add Advanced Markers Samples (#1435)
1 parent 49c4502 commit 8caba9c

File tree

10 files changed

+678
-248
lines changed

10 files changed

+678
-248
lines changed

ApiDemos/java/app/src/gms/AndroidManifest.xml

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<!--
3-
~ Copyright 2020 Google LLC
4-
~
5-
~ Licensed under the Apache License, Version 2.0 (the "License");
6-
~ you may not use this file except in compliance with the License.
7-
~ You may obtain a copy of the License at
8-
~
9-
~ https://www.apache.org/licenses/LICENSE-2.0
10-
~
11-
~ Unless required by applicable law or agreed to in writing, software
12-
~ distributed under the License is distributed on an "AS IS" BASIS,
13-
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
~ See the License for the specific language governing permissions and
15-
~ limitations under the License.
16-
-->
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
Copyright 2018 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
1716
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
1817

1918
<application
@@ -30,6 +29,9 @@
3029
<category android:name="android.intent.category.LAUNCHER" />
3130
</intent-filter>
3231
</activity>
32+
<activity
33+
android:name=".AdvancedMarkersDemoActivity"
34+
android:label="@string/advanced_markers_demo_label" />
3335
<activity
3436
android:name=".BasicMapDemoActivity"
3537
android:label="@string/basic_map_demo_label" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.example.mapdemo;
15+
16+
import android.graphics.Color;
17+
import android.os.Bundle;
18+
import android.util.Log;
19+
import android.widget.TextView;
20+
21+
import androidx.appcompat.app.AppCompatActivity;
22+
23+
import com.google.android.gms.maps.CameraUpdateFactory;
24+
import com.google.android.gms.maps.GoogleMap;
25+
import com.google.android.gms.maps.OnMapReadyCallback;
26+
import com.google.android.gms.maps.SupportMapFragment;
27+
import com.google.android.gms.maps.model.AdvancedMarkerOptions;
28+
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
29+
import com.google.android.gms.maps.model.LatLng;
30+
import com.google.android.gms.maps.model.MapCapabilities;
31+
import com.google.android.gms.maps.model.Marker;
32+
import com.google.android.gms.maps.model.PinConfig;
33+
34+
/**
35+
* The following sample showcases how to create Advanced Markers, and use all their customization
36+
* possibilities.
37+
*/
38+
public class AdvancedMarkersDemoActivity extends AppCompatActivity implements OnMapReadyCallback {
39+
40+
private static final LatLng SINGAPORE = new LatLng(1.3521, 103.8198);
41+
private static final LatLng KUALA_LUMPUR = new LatLng(3.1390, 101.6869);
42+
private static final LatLng JAKARTA = new LatLng(-6.2088, 106.8456);
43+
private static final LatLng BANGKOK = new LatLng(13.7563, 100.5018);
44+
private static final LatLng MANILA = new LatLng(14.5995, 120.9842);
45+
private static final LatLng HO_CHI_MINH_CITY = new LatLng(10.7769, 106.7009);
46+
47+
private static final float ZOOM_LEVEL = 3.5f;
48+
49+
private static final String TAG = AdvancedMarkersDemoActivity.class.getName();
50+
51+
@Override
52+
protected void onCreate(Bundle savedInstanceState) {
53+
super.onCreate(savedInstanceState);
54+
setContentView(R.layout.advanced_markers_demo);
55+
56+
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
57+
if (mapFragment != null) {
58+
mapFragment.getMapAsync(this);
59+
}
60+
}
61+
62+
@Override
63+
public void onMapReady(GoogleMap map) {
64+
map.moveCamera(CameraUpdateFactory.newLatLngZoom(SINGAPORE, ZOOM_LEVEL));
65+
66+
MapCapabilities capabilities = map.getMapCapabilities();
67+
Log.d(TAG, "Are advanced markers enabled? " + capabilities.isAdvancedMarkersAvailable());
68+
69+
// This sample sets a view as the iconView for the Advanced Marker
70+
TextView textView = new TextView(this);
71+
textView.setText("Hello!");
72+
Marker advancedMarkerView = map.addMarker(new AdvancedMarkerOptions()
73+
.position(SINGAPORE)
74+
.iconView(textView)
75+
.zIndex(1f));
76+
77+
// This uses PinConfig.Builder to create an instance of PinConfig.
78+
PinConfig.Builder pinConfigBuilder = PinConfig.builder();
79+
pinConfigBuilder.setBackgroundColor(Color.MAGENTA);
80+
PinConfig pinConfig = pinConfigBuilder.build();
81+
82+
// Use the PinConfig instance to set the icon for AdvancedMarkerOptions.
83+
AdvancedMarkerOptions advancedMarkerOptions = new AdvancedMarkerOptions()
84+
.icon(BitmapDescriptorFactory.fromPinConfig(pinConfig))
85+
.position(KUALA_LUMPUR);
86+
87+
// Pass the AdvancedMarkerOptions instance to addMarker().
88+
Marker marker = map.addMarker(advancedMarkerOptions);
89+
90+
// This sample changes the border color of the advanced marker
91+
PinConfig.Builder pinConfigBuilder2 = PinConfig.builder();
92+
pinConfigBuilder2.setBorderColor(Color.BLUE);
93+
PinConfig pinConfig2 = pinConfigBuilder2.build();
94+
95+
AdvancedMarkerOptions advancedMarkerOptions2 = new AdvancedMarkerOptions()
96+
.icon(BitmapDescriptorFactory.fromPinConfig(pinConfig2))
97+
.position(JAKARTA);
98+
99+
Marker marker2 = map.addMarker(advancedMarkerOptions2);
100+
101+
// Set the glyph text.
102+
PinConfig.Builder pinConfigBuilder3 = PinConfig.builder();
103+
PinConfig.Glyph glyphText = new PinConfig.Glyph("A");
104+
105+
// Alternatively, you can set the text color:
106+
// Glyph glyphText = new Glyph("A", Color.GREEN);
107+
pinConfigBuilder3.setGlyph(glyphText);
108+
PinConfig pinConfig3 = pinConfigBuilder3.build();
109+
110+
AdvancedMarkerOptions advancedMarkerOptions3 = new AdvancedMarkerOptions()
111+
.icon(BitmapDescriptorFactory.fromPinConfig(pinConfig3))
112+
.position(BANGKOK);
113+
114+
Marker marker3 = map.addMarker(advancedMarkerOptions3);
115+
116+
// Create a transparent glyph.
117+
PinConfig.Builder pinConfigBuilder4 = PinConfig.builder();
118+
pinConfigBuilder4.setBackgroundColor(Color.MAGENTA);
119+
pinConfigBuilder4.setGlyph(new PinConfig.Glyph(Color.TRANSPARENT));
120+
PinConfig pinConfig4 = pinConfigBuilder4.build();
121+
122+
AdvancedMarkerOptions advancedMarkerOptions4 = new AdvancedMarkerOptions()
123+
.icon(BitmapDescriptorFactory.fromPinConfig(pinConfig4))
124+
.position(MANILA);
125+
126+
Marker marker4 = map.addMarker(advancedMarkerOptions4);
127+
128+
// Collision behavior can only be changed in the AdvancedMarkerOptions object.
129+
// Changes to collision behavior after a marker has been created are not possible
130+
int collisionBehavior = AdvancedMarkerOptions.CollisionBehavior.REQUIRED_AND_HIDES_OPTIONAL;
131+
AdvancedMarkerOptions advancedMarkerOptions5 = new AdvancedMarkerOptions()
132+
.position(HO_CHI_MINH_CITY)
133+
.collisionBehavior(collisionBehavior);
134+
135+
Marker marker5 = map.addMarker(advancedMarkerOptions5);
136+
}
137+
}

0 commit comments

Comments
 (0)