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

구글맵v2 현재위치 지정에 대해 질문드립니다.

0 추천
액티비티를 시작할 시에 구글맵에 현재 저의 위치로 마커를 찍도록 해보려고 합니다

package com.example.gmap;

import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapActivity extends FragmentActivity {
    
    private GoogleMap gMap;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_activity);
        // TODO Auto-generated method stub
        init();
    }
    
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        init();
    }

    void init () {
        if (gMap==null) {
            gMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            if(gMap!=null) {
                addMarker();
            }
        }
    }
    
    void addMarker () {
        Location cLoc = gMap.getMyLocation();
        gMap.addMarker(new MarkerOptions().position(new LatLng(cLoc.getLatitude(),cLoc.getLongitude())).title("test"));
    }
}

10-02 11:27:42.031: E/AndroidRuntime(20030): FATAL EXCEPTION: main
10-02 11:27:42.031: E/AndroidRuntime(20030): Process: com.example.gmap, PID: 20030
10-02 11:27:42.031: E/AndroidRuntime(20030): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gmap/com.example.gmap.MapActivity}: java.lang.IllegalStateException: MyLocation layer not enabled
10-02 11:27:42.031: E/AndroidRuntime(20030):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at android.app.ActivityThread.access$1000(ActivityThread.java:198)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at android.os.Looper.loop(Looper.java:145)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at android.app.ActivityThread.main(ActivityThread.java:6837)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at java.lang.reflect.Method.invoke(Native Method)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at java.lang.reflect.Method.invoke(Method.java:372)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
10-02 11:27:42.031: E/AndroidRuntime(20030): Caused by: java.lang.IllegalStateException: MyLocation layer not enabled
10-02 11:27:42.031: E/AndroidRuntime(20030):     at com.google.a.a.ab.b(Unknown Source)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at com.google.maps.api.android.lib6.d.et.j(Unknown Source)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at com.google.android.gms.maps.internal.j.onTransact(SourceFile:281)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at android.os.Binder.transact(Binder.java:395)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.getMyLocation(Unknown Source)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at com.google.android.gms.maps.GoogleMap.getMyLocation(Unknown Source)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at com.example.gmap.MapActivity.addMarker(MapActivity.java:42)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at com.example.gmap.MapActivity.init(MapActivity.java:36)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at com.example.gmap.MapActivity.onCreate(MapActivity.java:22)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at android.app.Activity.performCreate(Activity.java:6500)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
10-02 11:27:42.031: E/AndroidRuntime(20030):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
10-02 11:27:42.031: E/AndroidRuntime(20030):     ... 10 more

위 소스에서 Location 을 이용하지않고 제가 직접 position에 (0,0)과 같이 특정값을 주어 실행하였을땐 런타임 오류없이 마커가 찍혀 나오는데요, Location에 getMyLocation 함수를 이용할 시엔 런타임 에러가 발생합니다...

초보개발자에게 도움부탁드립니다ㅜㅜ
멍청한놈 (220 포인트) 님이 2015년 10월 2일 질문

1개의 답변

0 추천
웁스...답변 달고 버튼 눌렀더니 DB접속 에러가 나면서 답변이 사라졌네요. ㅠ.ㅠ

대충 답변할께요.

getMyLocation()은 deprecated되었습니다. (https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap)

로그만으로 추정해보면, 현재 자기 위치를 얻어올 수 없는 상황인 것 같네요

setMyLocationEnabled(true)를 먼저 호출하고 자기 위치를 얻어와보시면 되지 않을까 싶습니다.

 

deprecated된 메소드는 가능하면 호출하지 않는 게 좋습니다. 물론 연습용이라면 상관없습니다만, 실제 동작해야 하는 서비스이라면 언제 해당 메소드에 대한 지원이 끊길지 모르니까요.
cc1232 (35,280 포인트) 님이 2015년 10월 2일 답변
cc1232님이 2015년 10월 2일 수정
답변감사합니다
답변주신대로 setMyLocation(true) 호출후에 해보았지만, cLoc에 null값이 저장되는건 변하지 않네요...
10-02 13:51:53.971: D/1(6734): 1
10-02 13:51:53.971: D/AndroidRuntime(6734): Shutting down VM
10-02 13:51:53.971: E/AndroidRuntime(6734): FATAL EXCEPTION: main
10-02 13:51:53.971: E/AndroidRuntime(6734): Process: com.example.gmap, PID: 6734
10-02 13:51:53.971: E/AndroidRuntime(6734): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gmap/com.example.gmap.MapActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
10-02 13:51:53.971: E/AndroidRuntime(6734):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119)
10-02 13:51:53.971: E/AndroidRuntime(6734):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
10-02 13:51:53.971: E/AndroidRuntime(6734):     at android.app.ActivityThread.access$1000(ActivityThread.java:198)
10-02 13:51:53.971: E/AndroidRuntime(6734):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
10-02 13:51:53.971: E/AndroidRuntime(6734):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-02 13:51:53.971: E/AndroidRuntime(6734):     at android.os.Looper.loop(Looper.java:145)
10-02 13:51:53.971: E/AndroidRuntime(6734):     at android.app.ActivityThread.main(ActivityThread.java:6837)
10-02 13:51:53.971: E/AndroidRuntime(6734):     at java.lang.reflect.Method.invoke(Native Method)
10-02 13:51:53.971: E/AndroidRuntime(6734):     at java.lang.reflect.Method.invoke(Method.java:372)
10-02 13:51:53.971: E/AndroidRuntime(6734):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
10-02 13:51:53.971: E/AndroidRuntime(6734):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
10-02 13:51:53.971: E/AndroidRuntime(6734): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
10-02 13:51:53.971: E/AndroidRuntime(6734):     at com.example.gmap.MapActivity.addMarker(MapActivity.java:46)
10-02 13:51:53.971: E/AndroidRuntime(6734):     at com.example.gmap.MapActivity.init(MapActivity.java:38)
10-02 13:51:53.971: E/AndroidRuntime(6734):     at com.example.gmap.MapActivity.onCreate(MapActivity.java:23)
10-02 13:51:53.971: E/AndroidRuntime(6734):     at android.app.Activity.performCreate(Activity.java:6500)
10-02 13:51:53.971: E/AndroidRuntime(6734):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
10-02 13:51:53.971: E/AndroidRuntime(6734):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
10-02 13:51:53.971: E/AndroidRuntime(6734):     ... 10 more

구글맵에서 현재 위치의 데이터 값 자체를 받는건 불가능한건가요?
해당 메소드 자체가 deprecated된거니까 그게 동작하지 않는다면, 권장되는 방식으로 사용해야죠
현재 구글에서 권장하는 방식은 구글맵이 아니라 플레이서비스를 통해 위치를 얻어오는 방식입니다.
https://developer.android.com/training/location/index.html
...