내 앱에 사용 된 코드는 다음과 같습니다. 나는 이것을 사용하여 사용자의 현재 위치를 얻었습니다.
public class AndroidLocationActivity {
String provider;
public double latitude, longitude = 0;
CurrentPositionTask getCurrentLocation;
Location currentLocation;
LocationListener locationListener;
LocationManager locationManager;
private long time=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
setCriteria();
currentLocation = AndroidLocationActivity.this.locationManager.getLastKnownLocation(provider);
if (currentLocation == null) {
currentLocation = new Location(provider);
}
time = currentLocation.getTime();
if (latitude == 0 && longitude == 0) {
latitude = currentLocation.getLatitude();
longitude = currentLocation.getLongitude();
}
} catch (NullPointerException e) {
// TODO: handle exception
System.out.println("Null");
e.printStackTrace();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
runAsyncTask();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
locationManager.removeUpdates(locationListener);
}
public void setCriteria() {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
provider = locationManager.getBestProvider(criteria, true);
if (provider == null) {
provider = LocationManager.GPS_PROVIDER;
}
}
public void runAsyncTask() {
// TODO Auto-generated method stub
if (getCurrentLocation == null) {
getCurrentLocation = new CurrentPositionTask();
}
if (getCurrentLocation != null) {
getCurrentLocation.execute("Searching for Location");
}
}
public boolean checkConnection()
{
//ARE WE CONNECTED TO THE NET
ConnectivityManager conMgr = (ConnectivityManager) getSystemService (AndroidLocationActivity.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable()&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
} else {
return false;
}
}
private class CurrentPositionTask extends AsyncTask<String, Void, Void>
{
private ProgressDialog Dialog = new ProgressDialog(AndroidLocationActivity.this);
private boolean flag = true;
public CurrentPositionTask() {
locationListener = new MyLocationListener();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
try {
if (checkConnection()) {
Dialog.setTitle("Loading");
Dialog.setMessage("Searching for Location");
Dialog.show();
locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
}
else {
Toast.makeText(getApplicationContext(), "Internet is Not Available", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
while (flag) {
if (latitude !=0 && longitude != 0) {
flag=false;
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (Dialog != null && Dialog.isShowing()) {
Dialog.dismiss();
Intent homeIntent = new Intent(AndroidLocationActivity.this.getApplicationContext(), nextactivityname.class);
여기에서 다음 활동이 시작됩니다.
homeIntent.putExtra("lat", latitude);
homeIntent.putExtra("lng", longitude);
startActivity(homeIntent);
}
locationManager.removeUpdates(locationListener);
}
}
class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
}
은 그래서 당신은 GPS의 도움으로 현재 위치를 얻을 수있는이 방법은 안드로이드에서 제공합니다. 희망이 도움이 될 것입니다 .....
프로젝트에서 정확히하고 싶은 것이 있습니까? 제발 좀 자세히 설명해주세요. LBS에서 android에서 일했습니다. 그래서 제게 당신의 프로젝트를 안내 할 수 있도록 좀 더 자세한 정보를주세요. – Scorpion
이 프로젝트에서 나는 안드로이드에서 실행되는 함대 관리 응용 프로그램을 개발해야합니다. 응용 프로그램은 GPS 수신기에서 위치 데이터를 가져 와서 Google지도에 표시합니다. – user1258686
기본적으로 위치 (즉, 위도와 경도)를 가져 오시겠습니까? – Scorpion