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

구글맵 시작위치 설정관하여 질문드립니다.

0 추천
구글맵v2 을 시작하면 바로 저의 현재위치로 이동하여 줌되도록 설정하려고 해봤습니다.

GooglePlayServicesUtil.isGooglePlayServicesAvailable(MainActivity.this);
        gMap = ((SupportMapFragment)getSupportFragmentManager().
                findFragmentById(R.id.map)).getMap();//프래그먼트 연결
        
        Location initLoc = gMap.getMyLocation();
        if(initLoc!=null){
               LatLng myLocation = new LatLng(initLoc.getLatitude(),initLoc.getLongitude());
               gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 10));

이런 식으로 코드를 짜봤는데, gmap 인스턴스 생성 부분까지만 두고 주석처리하여 실행하면 구글맵이 띄워지긴 하지만, 아래 로케이션을 이용해서 현재위치를 받아 카메라 이동시킬시엔 런타임 에러가납니다.

어느부분이 문제일까요...?
멍청한놈 (220 포인트) 님이 2015년 10월 1일 질문

1개의 답변

0 추천

getMap()이 deprecated되어서 gMap에 null이 들어가 있을 것 같습니다.

그래서 Location initLoc = gMap.getMyLocation(); 이 문장에서 Null Pointer Exception이 발생했겠죠

변경된 방식으로 맵을 얻어와야 합니다(아래는 변경된 방식에 대한 설명. 더 구체적인 부분은 구글 개발자 사이트에서 읽어보시길...). 

The Google Maps Android API also exposes a new getMapAsync(OnMapReadyCallback) method to MapFragment and MapViewwhich will notify you exactly when the map is ready. This serves as a replacement for the now deprecated getMap() method.

cc1232 (35,280 포인트) 님이 2015년 10월 1일 답변
...