Multiple line or break line in .snippet (google maps apiv2)
Asked Answered
C

3

11

I'm developing in google maps APIv2. The issue that I'm facing now is I only able to add in one line of word in info windows/snippet. But the output that I want is able to display in break line form as example show below. So is there any possible methods to solve it?

Example:

Coordinate: 03.05085, 101.70506

Speed Limit: 80 km/h

public class MainActivity extends FragmentActivity {
  static final LatLng SgBesi = new LatLng(03.05085, 101.76022);
  static final LatLng JB = new LatLng(1.48322, 103.69065);
  private GoogleMap map;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
            .getMap();

    map.addMarker(new MarkerOptions().position(SgBesi)
        .title("KM D7.7 Sungai Besi")               //name
        .snippet("Coordinate: 03.05085, 101.70506"));   //description

    map.addMarker(new MarkerOptions().position(JB)
        .title("test") 
        .snippet("Hey, how are you?")); 

       // .icon(BitmapDescriptorFactory  //set icon
       //     .fromResource(R.drawable.ic_launcher)));

    // move the camera instantly with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(SgBesi, 15));

    // zoom in, animating the camera.       
    map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
   }
}
Candlemas answered 30/6, 2013 at 14:35 Comment(1)
after I had gone through number of similar examples as the code provided above, I still unable to solve my problem. I'm not good in programming and not really understand on how should I specify a particular location with custom infowindow. Most of the examples that I had tested they did not specified a particular location. Can anyone post a complete coding for me to refer? I will really appreciated if anyone could help me solve part of my final year project issue that I have been tested out for 3 days. Thank you very much.Candlemas
S
22

You need to use a custom InfoWindowAdapter to change the layout of InfoWindow to a custom design defined using a layout file. Basically you need to :

  1. Declare your function using GoogleMap.setInfoWindowAdapter(Yourcustominfowindowadpater)
  2. Have a class like below:

...

class Yourcustominfowindowadpater implements InfoWindowAdapter {
    private final View mymarkerview;

    Yourcustominfowindowadpater() {
        mymarkerview = getLayoutInflater()
            .inflate(R.layout.custominfowindow, null); 
    }

    public View getInfoWindow(Marker marker) {      
        render(marker, mymarkerview);
        return mymarkerview;
    }

    public View getInfoContents(Marker marker) {
       return null;
    }

    private void render(Marker marker, View view) {
       // Add the code to set the required values 
       // for each element in your custominfowindow layout file
    }
}
Saladin answered 30/6, 2013 at 19:16 Comment(1)
@PSIXO: No, buttons won't work. From the docs: "The info window ... is not a live view .... will not respect ... touch or gesture events. However you can listen to a generic click event on the whole info window ...". So you could use 'whole info window events' to listen for clicks that would be over the button, but the button itself would just be some pixels on an image (so no button click animation either).Masonite
P
1

I would like to add something to @tony , According to documentation you cant add action to your custom view as it draw as image in run time.

Note: The info window that is drawn is not a live view. The view is rendered as an image (using View.draw(Canvas)) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (for example, after an image has loaded), call showInfoWindow(). Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.

Pastorship answered 31/12, 2015 at 9:11 Comment(0)
P
0

You will have to use InfoWindowAdapter to create custom info windows. The default implementation somehow removes newlines from snippet, which might be considered a minor bug.

Pollywog answered 30/6, 2013 at 19:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.