Added a marker to the map along with some informative window.

Marker marker = map.addMarker(new MarkerOptions() .position(new LatLng(47.045029, 28.861427)) .title("Marker") .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)) .snippet("Population: 4,137,400")); 

Implemented in InfoWindow when you click a button on which you can click. How can I add the ability to call by clicking on a certain number (for each marker has its own number)?

Map class:

 @Override public void onMapReady(GoogleMap map) { map.getUiSettings().setZoomControlsEnabled(true); LatLngBounds.Builder builder = new LatLngBounds.Builder(); //final MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(map); MapWrapperLayout mapWrapperLayout = (MapWrapperLayout) findViewById(R.id.map_relative_layout); //final GoogleMap map = mapFragment.getMap(); // MapWrapperLayout initialization // 39 - default marker height // 20 - offset between the default InfoWindow bottom edge and it's content bottom edge mapWrapperLayout.init(map, getPixelsFromDp(this, 39 + 20)); // We want to reuse the info window for all the markers, // so let's create only one class member instance this.infoWindow = (ViewGroup) getLayoutInflater().inflate(R.layout.info_window, null); this.infoTitle = (TextView) infoWindow.findViewById(R.id.title); this.infoSnippet = (TextView) infoWindow.findViewById(R.id.snippet); this.infoButton = (Button) infoWindow.findViewById(R.id.button); // Setting custom OnTouchListener which deals with the pressed state // so it shows up this.infoButtonListener = new OnInfoWindowElemTouchListener(infoButton, getResources().getDrawable(R.drawable.round_but_green_sel), //btn_default_normal_holo_light getResources().getDrawable(R.drawable.round_but_red_sel)) //btn_default_pressed_holo_light { @Override protected void onClickConfirmed(View v, Marker marker) { // Here we can perform some action triggered after clicking the button Toast.makeText(MapsActivity.this, marker.getTitle() + "'s button clicked!", Toast.LENGTH_SHORT).show(); } }; this.infoButton.setOnTouchListener(infoButtonListener); map.setInfoWindowAdapter(new InfoWindowAdapter() { @Override public View getInfoWindow(Marker marker) { return null; } @Override public View getInfoContents(Marker marker) { // Setting up the infoWindow with current's marker info infoTitle.setText(marker.getTitle()); infoSnippet.setText(marker.getSnippet()); infoButtonListener.setMarker(marker); mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow); return infoWindow; } }); Marker marker = map.addMarker(new MarkerOptions() .position(new LatLng(47.045029, 28.861427)) .title("Marker") .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)) .snippet("Population: 4,137,400")); Marker marker2 = map.addMarker(new MarkerOptions() .position(new LatLng(47.000327, 28.867950)) .title("Marker") .snippet("Population: 4,137,400")) builder.include(marker.getPosition()); builder.include(marker2.getPosition()); LatLngBounds bounds = builder.build(); int padding = 150; // offset from edges of the map in pixels CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding); map.moveCamera(cu); } 

The OnInfoWindowElemTouchListener class:

 public abstract class OnInfoWindowElemTouchListener implements OnTouchListener { private final View view; private final Drawable bgDrawableNormal; private final Drawable bgDrawablePressed; private final Handler handler = new Handler(); private Marker marker; private boolean pressed = false; public OnInfoWindowElemTouchListener(View view, Drawable bgDrawableNormal, Drawable bgDrawablePressed) { this.view = view; this.bgDrawableNormal = bgDrawableNormal; this.bgDrawablePressed = bgDrawablePressed; } public void setMarker(Marker marker) { this.marker = marker; } @Override public boolean onTouch(View vv, MotionEvent event) { if (0 <= event.getX() && event.getX() <= view.getWidth() && 0 <= event.getY() && event.getY() <= view.getHeight()) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: startPress(); break; // We need to delay releasing of the view a little so it shows the // pressed state on the screen case MotionEvent.ACTION_UP: handler.postDelayed(confirmClickRunnable, 150); break; case MotionEvent.ACTION_CANCEL: endPress(); break; default: break; } } else { // If the touch goes outside of the view's area // (like when moving finger out of the pressed button) // just release the press endPress(); } return false; } private void startPress() { if (!pressed) { pressed = true; handler.removeCallbacks(confirmClickRunnable); view.setBackgroundDrawable(bgDrawablePressed); if (marker != null) marker.showInfoWindow(); } } private boolean endPress() { if (pressed) { this.pressed = false; handler.removeCallbacks(confirmClickRunnable); view.setBackgroundDrawable(bgDrawableNormal); if (marker != null) marker.showInfoWindow(); return true; } else return false; } private final Runnable confirmClickRunnable = new Runnable() { public void run() { if (endPress()) { onClickConfirmed(view, marker); } } }; /** * This is called after a successful click */ protected abstract void onClickConfirmed(View v, Marker marker); } 
  • one
  • @pavlofff is a great answer, thanks, but this is only the first part of the question. How to implement the dialer itself by clicking on the button in the InfoWindow remains open. Do you think it is possible to update the question? - Inkognito

1 answer 1

Regarding the creation of clickable info:

stackoverflow.com/a/15040761 ? - pavlofff Feb 10 at 6:16 pm

Concerning a call on a clique:

 Intent call = new Intent(Intent.ACTION_DIAL); call.setData(Uri.parse("tel:" + phone)); //phone - переменная, в которой хранится номер телефона startActivity(call); 

This needs to be inserted into the post-click method.

UPD

You can store the phone number as follows:

 Marker marker = map.addMarker(new MarkerOptions() .position(new LatLng(47.045029, 28.861427)) .title("Marker") .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)) .snippet("Population: 4,137,400")); marker.setTag(phone); 

And use it like this:

 this.infoButtonListener = new OnInfoWindowElemTouchListener(infoButton, getResources().getDrawable(R.drawable.round_but_green_sel), //btn_default_normal_holo_light getResources().getDrawable(R.drawable.round_but_red_sel)) //btn_default_pressed_holo_light { @Override protected void onClickConfirmed(View v, Marker marker) { Object tag = marker.getTag(); if (tag != null){ Intent call = new Intent(Intent.ACTION_DIAL); call.setData(Uri.parse("tel:" + String.valueOf(tag))); startActivity(call); }else{ //обработать момент, когда номера нет, необходимо здесь } } }; 
  • Thank you, but if I understand correctly this is a general example, that is, for all markers, when pressed, it will be the same number. In the question there was a slight clarification on the occasion - "for each marker has its own number". - Inkognito
  • @Inkognito instead of the phone variable, use your variable in which the number is stored, just for this marker and then the calls will be to different numbers - zTrap
  • Well, I’ll insert your code snippet into the method where I click, respectively, for each click there will be the same number. I do not understand how I can then use it for each window separately? For example, there are parameters for the marker where I set the text / coordinates / snippet / icon. How can I push the number here as well? I'm sorry, but I really don't quite understand the design. - Inkognito
  • one
    @Inkognito yes, you can also send mail - zTrap
  • one
    @Inkognito look at all the overloads of the setTag method, one of them supports multiple tags - zTrap