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