0

I want my map showing toolbar without clicking the marker. But it is always hide until I click the marker. Please help me. I had try some way that i got from googling. But none working.

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)).icon(BitmapDescriptorFactory.defaultMarker()));
    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            mMap.getUiSettings().setMapToolbarEnabled(true);
            return false;
        }
    });
    mMap.getUiSettings().setZoomControlsEnabled(true);
    mMap.getUiSettings().setAllGesturesEnabled(false);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 16));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 16));
}

layout

<fragment
      android:id="@+id/mapFragment"
      class="com.google.android.gms.maps.SupportMapFragment"
      android:layout_width="match_parent"
      android:layout_height="200dp"/>

compile 'com.google.android.gms:play-services-maps:10.2.0'

compile 'com.google.android.gms:play-services-location:10.2.0'

Nicky Apriliani
  • 321
  • 4
  • 25
  • do u hv any idea how to fix it? @KayoLima – Nicky Apriliani May 30 '17 at 13:21
  • I can give you a sample in which you can create marker with text drawn on it. But the text drawing you may need to customize. If you want , I can put up the code. As per your requirement, it will show the value by default without clicking on Marker – Sreehari May 30 '17 at 14:02

3 Answers3

1

You can't manually show that, because the overlay that appears when the marker is clicked is created and destroyed on-the-spot implicity.

You can create an overlay over your map with 2 ImageView for example:

// Directions
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr=21.5, 0.15&daddr=-49.5, 0.15"));
startActivity(intent);
// Default google map
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?q=loc:51.5, 0.125"));
startActivity(intent);

change the coordinates based on your markers getPosition() and the user's location and call appropriate intents when clicked

Kayo Lima
  • 740
  • 7
  • 16
1

You can create a custom Marker with Icon and draw text as per your requirement.

NOTE:

But using the following sample, you need to customize according to your specific requirement

mGoogleMap.addMarker(new MarkerOptions()
          .position(LatLonPosition)
          .icon(BitmapDescriptorFactory
          .fromBitmap(writeTextOnImage(R.drawable.routemarker,"Your text"),this))));

Method to create drawable image

private Bitmap writeTextOnImage(int drawableId, String text,Context context) {
        Bitmap bitmapObj = BitmapFactory.decodeResource(getResources(), drawableId)
                .copy(Bitmap.Config.ARGB_8888, true);
        Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
        //Create Paint object
        //Define style attributes
        Paint lPaint = new Paint();
        lPaint.setStyle(Paint.Style.FILL);
        lPaint.setColor(Color.WHITE);
        lPaint.setTypeface(tf);
        lPaint.setTextAlign(Paint.Align.CENTER);
        lPaint.setTextSize(convertToPixels(context, 11));
        Rect textRect = new Rect();
        lPaint.getTextBounds(text, 0, text.length(), textRect);
        Canvas lCanvas = new Canvas(bm);
        if(textRect.width() >= (lCanvas.getWidth() - 4))     
        {
            lPaint.setTextSize(convertToPixels(context, 7)); 
        }
        int xPos = (lCanvas.getWidth() / 2) - 2;     
        int yPos = (int) ((lCanvas.getHeight() / 2)) ;
        lCanvas.drawText(text, xPos, yPos, lPaint);
        return  bitmapObj;
    }

Following will be the output

enter image description here

Sreehari
  • 5,621
  • 2
  • 25
  • 59
0

You have to move your code to outside the onClick method like this:

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.getUiSettings().setMapToolbarEnabled(true);
    mMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)).icon(BitmapDescriptorFactory.defaultMarker()));
    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {

            return false;
        }
    });
    mMap.getUiSettings().setZoomControlsEnabled(true);
    mMap.getUiSettings().setAllGesturesEnabled(false);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 16));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 16));
}
Luiz Fernando Salvaterra
  • 4,192
  • 2
  • 24
  • 42