Информация в окне маркера карты Google, показывающая все детали в одной строке - PullRequest
0 голосов
/ 26 января 2020

Я пытаюсь добавить окно маркера, когда пользователь нажимает на маркер, и все работает нормально, но во всплывающем окне вся информация отображается в одной строке, но я хочу показать каждое поле в другой строке. Пожалуйста, помогите мне, как я могу достичь этого?

 private void moveCamera(LatLng latLng, float zoom, PlaceInfo placeInfo){
    Log.d(TAG, "moveCamera: moving the camera to: lat: " + latLng.latitude + ", lng: " + latLng.longitude );
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));

    mMap.clear();
    if(placeInfo != null){
        try{
            String snippet = "Address: " + placeInfo.getAddress() + "\n" +
                    "Phone Number: " + placeInfo.getPhoneNumber() + "\n" +
                    "Website: " + placeInfo.getWebsiteUri() + "\n" +
                    "Price Rating: " + placeInfo.getRating() + "\n";

            MarkerOptions options = new MarkerOptions()
                    .position(latLng)
                    .title(placeInfo.getName())
                    .snippet(snippet);
            mMarker = mMap.addMarker(options);

        }catch (NullPointerException e){
            Log.e(TAG, "moveCamera: NullPointerException: " + e.getMessage() );
        }
    }else{
        mMap.addMarker(new MarkerOptions().position(latLng));
    }

    hideSoftKeyboard();
}

1 Ответ

0 голосов
/ 26 января 2020

Попробуйте создать CustomInfoWindowAdapter.

public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

 private final View mWindow;
 private Context mContext;

 public CustomInfoWindowAdapter(Context context) {
  mContext = context;
  mWindow = LayoutInflater.from(context).inflate(R.layout.custom_info_window, null);
 }

 private void rendowWindowText(Marker marker, View view) {

  String title = marker.getTitle();
  TextView tvTitle = (TextView) view.findViewById(R.id.title);

  if (!title.equals("")) {
   tvTitle.setText(title);
  }

  String snippet = marker.getSnippet();
  TextView tvSnippet = (TextView) view.findViewById(R.id.snippet);

  if (!snippet.equals("")) {
   tvSnippet.setText(snippet);
  }
 }

 @Override
 public View getInfoWindow(Marker marker) {
  rendowWindowText(marker, mWindow);
  return mWindow;
 }

 @Override
 public View getInfoContents(Marker marker) {
  rendowWindowText(marker, mWindow);
  return mWindow;
 }
}

И вызовите этот адаптер в своей деятельности.

yourMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(YourActivity.this));
...