728x90

전 포스팅을 안보셨다면 먼저 보시는 것을 추천드립니다.

 

안드로이드 스튜디오 주소명으로 위도/경도 값 구하기

전 포스팅에서 보시면 LatLng을 사용하려면 위도 경도 값을 넣어줘야 마커를 사용할 수 있습니다. 안드로이드 스튜디오 구글 맵 사용하기 먼저 구글 맵을 사용하기 전에 구글 API 사이트로 들어가 줍니다. Google..

1d1cblog.tistory.com

 

안드로이드 스튜디오 구글 맵 사용하기

먼저 구글 맵을 사용하기 전에 구글 API 사이트로 들어가 줍니다. Google Cloud Platform 하나의 계정으로 모든 Google 서비스를 Google Cloud Platform을 사용하려면 로그인하세요. accounts.google.com 좌측 상..

1d1cblog.tistory.com

onMapReady 메소드 안에 addMarker 메소르를 이용해 마커를 추가해 줄 수 있습니다.

 

현재 아래 코드는 clinic 객체 개수만큼의 마커를 찍고 있습니다.

public void onMapReady(final GoogleMap googleMap) {
        mgoogleMap = googleMap;

        googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                Log.d(TAG, "Load");
                LatLng latLng = new LatLng(addrToPoint(context, "서울시청").getLatitude(), addrToPoint(context, "서울시청").getLongitude());
                mgoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                mgoogleMap.animateCamera(CameraUpdateFactory.zoomTo(12));
            }
        });

        for(int i = 0 ; i < clinics.size(); i++) {
            Log.d(TAG,"create" + String.valueOf(i));
            Location location = addrToPoint(context, clinics.get(i).getAddress());
            LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            mgoogleMap.addMarker(markerOptions);
        }
}

MarkerOption에 대한 설정은 아래와 같습니다.

  • alpha : 마커의 투명도
  • anchor : 마커의 위치
  • title : 마커를 눌렀을 때 나타나는 제목
  • Snippet : 마커의 제목 아래 부가 텍스트
  • icon : 기본 마커 대신 사용할 이미지를 설정

마커 클릭시 알림창(Alert Dialog)를 사용하기 위해선 아래의 코드처럼 하면 됩니다.

 

마커를 여러개 사용하고 있기때문에 마커의 Title을 이용해서 식별하려고 합니다.

 

Clinic 객체에 Title을 매개변수로 받아서 연번을 반환하는 메소드를 만들어줍니다.

public String findIndex(String name) {
        if(this.name.equals(name)) {
            return number;
        }
        else return null;
    }

마커 클릭 시 나오는 Title 안내문을 클릭 시에 Alert Dialog를 띄우도록 했습니다.

mgoogleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                String marker_number = null;
                for(int i = 0 ; i < clinics.size() ; i++ ) {
                    if(clinics.get(i).findIndex(marker.getTitle()) != null) {
                        marker_number = clinics.get(i).findIndex(marker.getTitle());
                        Log.d(TAG, "marker_number " + marker_number);
                    }
                }
                final int marker_ID_number = Integer.parseInt(marker_number);
                Log.d(TAG, "marker number = " + String.valueOf(marker_ID_number));
                Log.d(TAG, "marker clinic name = " + clinics.get(marker_ID_number).getName());
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("병원정보");
                builder.setMessage(
                        "이름 : " + clinics.get(marker_ID_number-1).getName() +
                                "\n주소 : " + clinics.get(marker_ID_number-1).getAddress() +
                                "\n병원전화번호 : " + clinics.get(marker_ID_number-1).getPhoneNumber() +
                                "\n검체채취가능여부 : " + clinics.get(marker_ID_number-1).getSample()
                );
                builder.setPositiveButton("확인", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNegativeButton("전화걸기", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel" + clinics.get(Integer.parseInt(marker_ID_number)).getPhoneNumber())));
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });// 마커 클릭 시 Alert Dialog가 나오도록 설정

728x90

+ Recent posts