Program Club

Android Google Maps v2 : 여러 줄 스 니펫으로 마커를 추가하는 방법은 무엇입니까?

proclub 2020. 11. 22. 20:39
반응형

Android Google Maps v2 : 여러 줄 스 니펫으로 마커를 추가하는 방법은 무엇입니까?


아무도 Google지도 마커에 여러 줄 스 니펫을 추가하는 방법을 알고 있습니까? 마커를 추가하는 코드는 다음과 같습니다.

map.getMap().addMarker(new MarkerOptions()
    .position(latLng()).snippet(snippetText)
    .title(header).icon(icon));

스 니펫이 다음과 같이 보이기를 원합니다.

| HEADER |
|foo     |
|bar     |

그러나 snippetText를 "foo \ n bar"로 설정하려고 할 때 여러 줄로 foo bar만드는 방법에 대한 아이디어가 없습니다. 도와주세요?


이 작업을 수행하려면 고유 한 "정보 창"콘텐츠를 만들어야하는 것 같습니다.

  1. 프레임 에 들어갈 내용을 반환하기 위해 InfoWindowAdapter재정 의 구현을 만듭니다.getInfoContents()InfoWindow

  2. 전화 setInfoWindowAdapter()GoogleMap의 인스턴스를 전달하여InfoWindowAdapter

이 샘플 프로젝트 는 기술을 보여줍니다. 내 스 니펫을 "foo\nbar"올바르게 교체 하면 개행 문자가 처리됩니다. 그러나 더 가능성이 높으면 TextView원하는 시각적 결과에서 각 줄에 대해 별도의 위젯을 사용하여 줄 바꿈이 필요하지 않은 레이아웃을 얻을 수 있습니다.


다음과 같이 가장 쉬운 방법으로 수행했습니다.

private GoogleMap mMap;

Google지도에 마커추가 하는 동안 :

LatLng mLatLng = new LatLng(YourLatitude, YourLongitude);

mMap.addMarker(new MarkerOptions().position(mLatLng).title("My Title").snippet("My Snippet"+"\n"+"1st Line Text"+"\n"+"2nd Line Text"+"\n"+"3rd Line Text").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

그 후 Google MapInfoWindow 어댑터대한 코드를 입력하십시오 .

mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

      @Override
      public View getInfoWindow(Marker arg0) {
         return null;
      }

      @Override
      public View getInfoContents(Marker marker) {

        LinearLayout info = new LinearLayout(mContext);
        info.setOrientation(LinearLayout.VERTICAL);

        TextView title = new TextView(mContext);
        title.setTextColor(Color.BLACK);
        title.setGravity(Gravity.CENTER);
        title.setTypeface(null, Typeface.BOLD);
        title.setText(marker.getTitle());

        TextView snippet = new TextView(mContext);
        snippet.setTextColor(Color.GRAY);
        snippet.setText(marker.getSnippet());

        info.addView(title);
        info.addView(snippet);

      return info;
    }
});

도움이되기를 바랍니다.


바탕 Hiren 파텔의 대답 앤드류 S가 제안 :

 mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {

            Context context = getApplicationContext(); //or getActivity(), YourActivity.this, etc.

            LinearLayout info = new LinearLayout(context);
            info.setOrientation(LinearLayout.VERTICAL);

            TextView title = new TextView(context);
            title.setTextColor(Color.BLACK);
            title.setGravity(Gravity.CENTER);
            title.setTypeface(null, Typeface.BOLD);
            title.setText(marker.getTitle());

            TextView snippet = new TextView(context);
            snippet.setTextColor(Color.GRAY);
            snippet.setText(marker.getSnippet());

            info.addView(title);
            info.addView(snippet);

            return info;
        }
    });

mMap.setOnMapClickListener (new GoogleMap.OnMapClickListener () {

        @Override
        public void onMapClick(LatLng point) {

            // Already two locations
            if (markerPoints.size() > 1) {
                markerPoints.clear();
                mMap.clear();
            }

            // Adding new item to the ArrayList
            markerPoints.add(point);

            // Creating MarkerOptions
            MarkerOptions options = new MarkerOptions();

            // Setting the position of the marker

            options.position(point);
            if (markerPoints.size() == 1) {
                options.icon(BitmapDescriptorFactory.fromResource(R.mipmap.markerss)).title("Qtrip").snippet("Balance:\nEta:\nName:");
                options.getInfoWindowAnchorV();
            } else if (markerPoints.size() == 2) {
                options.icon(BitmapDescriptorFactory.fromResource(R.mipmap.markerss)).title("Qtrip").snippet("End");
            }
            mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

                @Override
                public View getInfoWindow(Marker arg0) {
                    return null;
                }

                @Override
                public View getInfoContents(Marker marker) {

                    Context context = getApplicationContext(); //or getActivity(), YourActivity.this, etc.

                    LinearLayout info = new LinearLayout(context);
                    info.setOrientation(LinearLayout.VERTICAL);

                    TextView title = new TextView(context);
                    title.setTextColor(Color.BLACK);
                    title.setGravity(Gravity.CENTER);
                    title.setTypeface(null, Typeface.BOLD);
                    title.setText(marker.getTitle());

                    TextView snippet = new TextView(context);
                    snippet.setTextColor(Color.GRAY);
                    snippet.setText(marker.getSnippet());

                    info.addView(title);
                    info.addView(snippet);

                    return info;
                }
            });
            mMap.addMarker(options);

참고URL : https://stackoverflow.com/questions/13904651/android-google-maps-v2-how-to-add-marker-with-multiline-snippet

반응형