Android:如何在onMapReady方法中添加Url链接到Marker?

时间:2018-02-12 12:21:51

标签: android google-maps-api-3 google-maps-markers android-maps

我使用地图API创建了地图,并使用纬度和经度坐标设置了标记。我不知道如何设置Url链接以在单击标记时移动到浏览器。

由于我有这么多标记,每个标记应移动到差异网址,怎么可能?

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));}

2 个答案:

答案 0 :(得分:0)

考虑mMap是你的地图对象。

mMap.setOnMarkerClickListener(new OnMarkerClickListener()
            {

                @Override
                public boolean onMarkerClick(Marker arg0) {
                    String url = "http://www.example.com";
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                    return true;
                }

            });  

答案 1 :(得分:0)

您必须在活动中或您拥有谷歌地图的片段中实施标记onClick事件

请查看下面的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);
        }
    }
}