본문 바로가기

안드로이드

Naver Map API를 이용한 현재위치 찾기

반응형

- 네이버 맵 API를 이용해서 현재 위도 경도를 얻어오고,  위도경도를 주소로 변환하는 방법에 대해서 포스팅합니다.

- 네이버 맵 소스안에 다 있는 내용을 그대로 가져온거라서 필요없는 내용도 있습니다.

- 네이버 개발자센터에서 지도 API키를 발급받습니다.(구글맵은 키받기가 엄청 복잡한데..네이버는 간편하네요)

-  public static final String API_KEY 에 발급받은 API키를 넣어주심됩니다.

- 네이버 지도 라이브러리 nmaps.jar를 iibs 폴더에 넣어줍니다.

- 주소는 동까지만 나오는데요.. 상세한 주소얻는 방법은 없나보네요?


----------------------------------------------------------------------------------------------


public class NaverMyLocation extends NMapActivity implements

OnMapStateChangeListener, OnCalloutOverlayListener {


// API-KEY

public static final String API_KEY = "159192585e12865f56dac5ec2e1ecb77";

// 네이버 맵 객체

NMapView mMapView = null;

// 맵 컨트롤러

NMapController mMapController = null;

// 맵을 추가할 레이아웃

LinearLayout MapContainer;


// 오버레이의 리소스를 제공하기 위한 객체

// NMapViewerResourceProvider mMapViewerResourceProvider = null;

// 오버레이 관리자

private NMapMyLocationOverlay mMyLocationOverlay;

private NMapLocationManager mMapLocationManager;

private NMapCompassManager mMapCompassManager;




@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// setContentView(R.layout.activity_course_01);

// 네이버 지도를 넣기 위한 LinearLayout 컴포넌트

// MapContainer = (LinearLayout) findViewById(R.id.MapContainer);


// 네이버 지도 객체 생성

mMapView = new NMapView(this);


// 지도 객체로부터 컨트롤러 추출

// mMapController = mMapView.getMapController();


// 네이버 지도 객체에 APIKEY 지정

mMapView.setApiKey(API_KEY);


// 생성된 네이버 지도 객체를 LinearLayout에 추가시킨다.

// MapContainer.addView(mMapView);


// 지도를 터치할 수 있도록 옵션 활성화

mMapView.setClickable(true);


// 확대/축소를 위한 줌 컨트롤러 표시 옵션 활성화

mMapView.setBuiltInZoomControls(true, null);

super.setMapDataProviderListener(onDataProviderListener);


// 지도에 대한 상태 변경 이벤트 연결

mMapView.setOnMapStateChangeListener(this);


}



@Override

public void onMapInitHandler(NMapView mapview, NMapError errorInfo) {

if (errorInfo == null) { // success

startMyLocation();//현재위치로 이동

// mMapController.setMapCenter(new NGeoPoint(126.978371,

// 37.5666091),

// 11);

} else { // fail

android.util.Log.e("NMAP",

"onMapInitHandler: error=" + errorInfo.toString());

}

}



@Override

public void onZoomLevelChange(NMapView mapview, int level) {

}



@Override

public void onMapCenterChange(NMapView mapview, NGeoPoint center) {

}


@Override

public void onAnimationStateChange(NMapView arg0, int animType,

int animState) {

}


@Override

public void onMapCenterChangeFine(NMapView arg0) {

}


@Override

public NMapCalloutOverlay onCreateCalloutOverlay(NMapOverlay arg0,

NMapOverlayItem arg1, Rect arg2) {


return null;

}




private void startMyLocation() {


mMapLocationManager = new NMapLocationManager(this);

mMapLocationManager

.setOnLocationChangeListener(onMyLocationChangeListener);

boolean isMyLocationEnabled = mMapLocationManager

.enableMyLocation(true);

if (!isMyLocationEnabled) {

Toast.makeText(

NaverMyLocation.this,

"Please enable a My Location source in system settings",

Toast.LENGTH_LONG).show();


Intent goToSettings = new Intent(

Settings.ACTION_LOCATION_SOURCE_SETTINGS);

startActivity(goToSettings);

finish();

}else{

}


//

// // compass manager

// mMapCompassManager = new NMapCompassManager(this);

//

// // create my location overlay

// mMyLocationOverlay = mOverlayManager.createMyLocationOverlay(

// mMapLocationManager, mMapCompassManager);

//

// if (mMyLocationOverlay != null) {

// if (!mOverlayManager.hasOverlay(mMyLocationOverlay)) {

// mOverlayManager.addOverlay(mMyLocationOverlay);

// }

//

// if (mMapLocationManager.isMyLocationEnabled()) {

//

// if (!mMapView.isAutoRotateEnabled()) {

// mMyLocationOverlay.setCompassHeadingVisible(true);

//

// mMapCompassManager.enableCompass();

//

// mMapView.setAutoRotateEnabled(true, false);

//

// MapContainer.requestLayout();

// } else {

// stopMyLocation();

// }

//

// mMapView.postInvalidate();

// } else {

// boolean isMyLocationEnabled = mMapLocationManager

// .enableMyLocation(true);

// if (!isMyLocationEnabled) {

// Toast.makeText(

// NaverMyLocation.this,

// "Please enable a My Location source in system settings",

// Toast.LENGTH_LONG).show();

//

// Intent goToSettings = new Intent(

// Settings.ACTION_LOCATION_SOURCE_SETTINGS);

// startActivity(goToSettings);

//

// return;

// }

// }

// }

}


private void stopMyLocation() {

if (mMyLocationOverlay != null) {

mMapLocationManager.disableMyLocation();


if (mMapView.isAutoRotateEnabled()) {

mMyLocationOverlay.setCompassHeadingVisible(false);


mMapCompassManager.disableCompass();


mMapView.setAutoRotateEnabled(false, false);


MapContainer.requestLayout();

}

}

}

private final NMapActivity.OnDataProviderListener onDataProviderListener = new NMapActivity.OnDataProviderListener() {


@Override

public void onReverseGeocoderResponse(NMapPlacemark placeMark, NMapError errInfo) {


if (errInfo != null) {

Log.e("myLog", "Failed to findPlacemarkAtLocation: error=" + errInfo.toString());

Toast.makeText(NaverMyLocation.this, errInfo.toString(), Toast.LENGTH_LONG).show();

return;

}else{

Toast.makeText(NaverMyLocation.this, placeMark.toString(), Toast.LENGTH_LONG).show();

}


}


};



private final NMapLocationManager.OnLocationChangeListener onMyLocationChangeListener = new NMapLocationManager.OnLocationChangeListener() {


@Override

public boolean onLocationChanged(NMapLocationManager locationManager,

NGeoPoint myLocation) {


// if (mMapController != null) {

// mMapController.animateTo(myLocation);

// }

Log.d("myLog", "myLocation  lat " + myLocation.getLatitude());

Log.d("myLog", "myLocation  lng " + myLocation.getLongitude());



findPlacemarkAtLocation(myLocation.getLongitude(), myLocation.getLatitude());

 //위도경도를 주소로 변환

 

return true;

}


@Override

public void onLocationUpdateTimeout(NMapLocationManager locationManager) {


// stop location updating

// Runnable runnable = new Runnable() {

// public void run() {

// stopMyLocation();

// }

// };

// runnable.run();


Toast.makeText(NaverMyLocation.this,

"Your current location is temporarily unavailable.",

Toast.LENGTH_LONG).show();

}


@Override

public void onLocationUnavailableArea(

NMapLocationManager locationManager, NGeoPoint myLocation) {


Toast.makeText(NaverMyLocation.this,

"Your current location is unavailable area.",

Toast.LENGTH_LONG).show();


stopMyLocation();

}


};

}

----------------------------------------------------------------------------------------------

반응형