마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

구글맵 여러개의 마커에 말풍선 모두 띄우기

0 추천

구글맵 에서 마커를 여러개 생성하고 말풍선을 항상 띄워놓기 위해 showInfoWindow() 를 사용했습니다.

그런데 마지막에 실행된 마커 하나만 말풍선이 나타나고 나머지는 사라집니다.

여러개의 마커에 모두 동시에 말풍선을 띄우는 방법은 없나요ㅜㅜ

 

public class FindMartActivity extends AppCompatActivity implements OnMapReadyCallback{
    private GoogleMap googleMap;
    private Marker curMarker;
    private LatLng point = new LatLng(36.83456344318653, 127.17946583991449);

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

        MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        try {
            MapsInitializer.initialize(this);
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }

    }

    public void onMapReady(GoogleMap map) {
        googleMap = map;
        showAllMart();
        startLocationService();
    }

    private void startLocationService() {
        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        GPSListener gpsListener = new GPSListener();
        long minTime = 10000;
        float minDistance = 0;

        try {
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    minTime,
                    minDistance,
                    gpsListener
            );

            Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if(lastLocation != null) {
                Double latitude = lastLocation.getLatitude();
                Double longitude = lastLocation.getLongitude();

                Toast.makeText(getApplicationContext(), "Last Known Location: " + "Latitude: " + latitude + "\nLongitude: " + longitude, Toast.LENGTH_SHORT).show();
                curMarker = googleMap.addMarker(new MarkerOptions()
                        .position(new LatLng(latitude, longitude))
                        .title("최근 위치")
                );
                curMarker.showInfoWindow();
                googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 13), 2000, null);
            }
        }catch(SecurityException ex) {
            System.out.println(ex.getMessage());
            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
        }

        Toast.makeText(getApplicationContext(), "위치확인이 시작됨, 로그확인", Toast.LENGTH_SHORT).show();

    }

    private class GPSListener implements LocationListener {
        public void onLocationChanged(Location location) {
            Double latitude = location.getLatitude();
            Double longitude = location.getLongitude();

            String msg = "Latitude: " + latitude + "\nLongitude: " + longitude;
            Log.i("GPSLocationService", msg);

            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();

            showCurrentLocation(latitude, longitude);
            //showCurrentLocation(36.83456344318653, 127.17946583991449);
        }

        public void onProviderDisabled(String provider) {
        }
        public void onProviderEnabled(String provider) {
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }

    private void showCurrentLocation(Double latitude, Double longitude) {
        LatLng curPoint = new LatLng(latitude, longitude);

        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(curPoint, 13), 2000, null);
        curMarker.remove();
        curMarker = googleMap.addMarker(new MarkerOptions()
                                    .position(curPoint)
                                    .title("현 위치")
                    );
        curMarker.showInfoWindow();

        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        //showAllMart();
    }

    private void showAllMart() {
        Marker aMarker, bMarker, cMarker;
        LatLng amartPoint = new LatLng(36.8196850, 127.1543900);
        LatLng bmartPoint = new LatLng(36.7991010, 127.1655390);
        LatLng cmartPoint = new LatLng(36.8262460, 127.1400880);

        aMarker = googleMap.addMarker(new MarkerOptions()
                .position(amartPoint)
                .title("A 마트")
        );
        aMarker.showInfoWindow();

        bMarker = googleMap.addMarker(new MarkerOptions()
                .position(bmartPoint)
                .title("B 마트")

        );
        bMarker.showInfoWindow();

        cMarker = googleMap.addMarker(new MarkerOptions()
                .position(cmartPoint)
                .title("C 마트")

        );
        cMarker.showInfoWindow();
    }

    public void onBackPressed() {
        super.onBackPressed();
    }
}

 

익명사용자 님이 2016년 11월 22일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...