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

안드로이드 gps 값 받아서 앱 화면에 계속 보여주기

0 추천
package com.android.location1;

import java.io.*;
import java.util.*;
import android.app.*;
import android.os.Bundle;
import android.content.*;
import android.location.*;
import android.util.Log;
import android.view.*;
import android.widget.Button;
import android.widget.TextView;

public class LocationDemo extends Activity implements LocationListener {

	private LocationManager locManager;
	Geocoder geoCoder;
	private Location myLocation = null;
	double latPoint = 0;
	double lngPoint = 0;
	float speed = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// LocationListener의 핸들을 얻음
		locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		// GPS로 부터 위치정보를 업데이트 요청, 1초마다 5km 이동시
		locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,
				0, this);

		// 기지국으로 부터 위치정보를 업데이트 요청
		locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
				1000, 0, this);

		// 주소를 확인하기 위한 Geocoder KOREA 와 KOREAN 둘다 가능
		geoCoder = new Geocoder(this, Locale.KOREAN);

		final Button gpsButton = (Button) findViewById(R.id.gpsButton);
		gpsButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {

				GetLocations();
				Log.d("location", "button pressed");

			}
		});

	}

	public void GetLocations() {

		// 텍스트뷰를 찾음
		TextView latText = (TextView) findViewById(R.id.tvLatitude);
		TextView lngText = (TextView) findViewById(R.id.tvLongitude);
		TextView speedText = (TextView) findViewById(R.id.tvSpeed);
		TextView jusoText = (TextView) findViewById(R.id.tvAddress);
		StringBuffer juso = new StringBuffer();

		if (myLocation != null) {
			latPoint = myLocation.getLatitude();
			lngPoint = myLocation.getLongitude();
			speed = (float) (myLocation.getSpeed() * 3.6);

			try {
				// 위도,경도를 이용하여 현재 위치의 주소를 가져온다.
				List<Address> addresses;
				addresses = geoCoder.getFromLocation(latPoint, lngPoint, 1);
				for (Address addr : addresses) {
					int index = addr.getMaxAddressLineIndex();
					for (int i = 0; i <= index; i++) {
						juso.append(addr.getAddressLine(i));
						juso.append(" ");
					}
					juso.append("\n");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

		latText.setText(String.valueOf(latPoint));
		lngText.setText(String.valueOf(lngPoint));
		speedText.setText(String.valueOf(speed));
		jusoText.setText(String.valueOf(juso));

	}

	public void onLocationChanged(Location location) {
		Log.d("location", "location changed");
		myLocation = location;
	}

	public void onProviderDisabled(String s) {

	}

	public void onProviderEnabled(String s) {

	}

	public void onStatusChanged(String s, int i, Bundle bundle) {

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			finish();
			System.exit(0);
		case KeyEvent.KEYCODE_HOME:
			ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
			am.restartPackage(getPackageName());
		}
		return super.onKeyDown(keyCode, event);
	}

}

-현재 실시간이 아닌 버튼으로 좌표를 가지고 오는데 시간을 두고 계속 값을 표기하는 방법이 있나요?

드이로드안보초 (430 포인트) 님이 2018년 5월 29일 질문
해결했습니다. 감사합니다!!

답변 달기

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