Search code examples
androidgoogle-maps-api-3google-maps-markersandroid-maps

Android: How to add Url link to the Marker in onMapReady method?


I have created a map using map API and have set marker using latitude and longitude coordinates. I have no idea how to set a Url link to move to a browser on clicking the marker.

Since I have so many marker each marker should move to a diff url, how it is possible?

public void onMapReady(GoogleMap googleMap)
{
    mMap = googleMap;
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
     {
        mMap.setMyLocationEnabled(true);
        Toast.makeText(MapsActivity.this,"Tap on Zoom button to view the Current Location",Toast.LENGTH_LONG).show();
        }
    else {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_FINE_LOCATION);
        }
    }

    mMap.addMarker(new MarkerOptions().position(
            new LatLng(12.9969284, 80.25792380000007)).title("A2B Car Parking").icon(
            BitmapDescriptorFactory.fromBitmap(BitmapFactory
                    .decodeResource(getResources(),
                            R.drawable.red_icon))).anchor(0.5f, 1f));

    mMap.addMarker(new MarkerOptions().position(
            new LatLng(13.0327601, 80.27573810000001)).title("Free Parking Place").icon(
            BitmapDescriptorFactory.fromBitmap(BitmapFactory
                    .decodeResource(getResources(),
                            R.drawable.red_icon))).anchor(0.5f, 1f));

    mMap.addMarker(new MarkerOptions().position(
            new LatLng(13.0414623,80.24994960000004)).icon(
            BitmapDescriptorFactory.fromBitmap(BitmapFactory
                    .decodeResource(getResources(),
                            R.drawable.red_icon))).anchor(0.5f, 1f));}

Solution

  • You have to implement the marker onClick event inside your activity or in your fragment where you have google map

    please check below snipet

    public class MapActivity extends android.support.v4.app.FragmentActivity
            implements OnMarkerClickListener
    {
        private Marker myMarker;
        private HashMap<Marker,String> hashmap = new HashMap<>();
    
        private void setUpMap()
        {
                        .......
            googleMap.setOnMarkerClickListener(this);
    
            myMarker = googleMap.addMarker(new MarkerOptions()
                    .position(latLng)
                    .title("My Spot")
                    .snippet("This is my spot!")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
            hashmap.put(myMarker,url);
        }
    
        @Override
        public boolean onMarkerClick(final Marker marker) {
    
            if (marker.equals(myMarker))
            {
                Uri uriUrl = Uri.parse(hashmap.get(myMarker));
                Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
                startActivity(launchBrowser);
            }
        }
    }