package ayh.mygps;
import java.util.Date;
//Download by http://www.codefans.net
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
//import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class mainatt extends Activity implements OnClickListener
{
private LocationManager lm;
private Location loc;
private Criteria ct;
private String provider;
private TextView tvLatitude;
private TextView tvLongitude;
private TextView tvHigh;
private TextView tvDirection;
private TextView tvSpeed;
private TextView tvGpsTime;
private TextView tvInfoType;
private EditText etSetTimeSpace;
private Button btnmanual;
private Button btnsettimespace;
private Button btnexit;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvLatitude = (TextView) findViewById(R.id.tvlatitude);
tvLongitude = (TextView) findViewById(R.id.tvlongitude);
tvHigh = (TextView) findViewById(R.id.tvhigh);
tvDirection = (TextView) findViewById(R.id.tvdirection);
tvSpeed = (TextView) findViewById(R.id.tvspeed);
tvGpsTime = (TextView) findViewById(R.id.tvgpstime);
tvInfoType = (TextView) findViewById(R.id.tvinfotype);
etSetTimeSpace = (EditText) findViewById(R.id.ettimespace);
btnmanual = (Button) findViewById(R.id.btnmanual);
btnmanual.setOnClickListener(this);
btnsettimespace = (Button) findViewById(R.id.btnsettimespace);
btnsettimespace.setOnClickListener(this);
btnexit = (Button) findViewById(R.id.btnexit);
btnexit.setOnClickListener(this);
initLocation();
}
//Download by http://www.codefans.net
private final LocationListener locationListener = new LocationListener()
{
@Override
public void onLocationChanged(Location arg0)
{
showInfo(getLastPosition(), 2);
}
@Override
public void onProviderDisabled(String arg0)
{
showInfo(null, -1);
}
@Override
public void onProviderEnabled(String arg0)
{
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2)
{
}
};
private void initLocation()
{
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
ct = new Criteria();
ct.setAccuracy(Criteria.ACCURACY_FINE);// 高精度
ct.setAltitudeRequired(true);// 显示海拔
ct.setBearingRequired(true);// 显示方向
ct.setSpeedRequired(true);// 显示速度
ct.setCostAllowed(false);// 不允许有花费
ct.setPowerRequirement(Criteria.POWER_LOW);// 低功耗
provider = lm.getBestProvider(ct, true);
// 位置变化监听,默认5秒一次,距离10米以上
lm.requestLocationUpdates(provider, 5000, 10, locationListener);
} else
showInfo(null, -1);
}
private gpsdata getLastPosition()
{
gpsdata result = new gpsdata();
loc = lm.getLastKnownLocation(provider);
if (loc != null)
{
//result.Latitude = (int) (loc.getLatitude() );
//result.Longitude = (int) (loc.getLongitude() );
result.Latitude = loc.getLatitude();
result.Longitude = loc.getLongitude();
result.High = loc.getAltitude();
result.Direct = loc.getBearing();
result.Speed = loc.getSpeed();
Date d = new Date();
d.setTime(loc.getTime());// UTC时间,转北京时间+8小时
result.GpsTime = DateFormat.format("yyyy-MM-dd kk:mm:ss", d).toString();
d = null;
}
return result;
}
private void showInfo(gpsdata cdata, int infotype)
{
if (cdata == null)
{
if (infotype == -1)
{
tvLatitude.setText("GPS功能已关闭");
tvLongitude.setText("");
tvHigh.setText("");
tvDirection.setText("");
tvSpeed.setText("");
tvGpsTime.setText("");
tvInfoType.setText("");
btnmanual.setEnabled(false);
btnsettimespace.setEnabled(false);
etSetTimeSpace.setEnabled(false);
}
} else
{
tvLatitude.setText(String.format("纬度:%f", cdata.Latitude));
tvLongitude.setText(String.format("经度:%f", cdata.Longitude));
tvHigh.setText(String.format("海拔:%f", cdata.High));
tvDirection.setText(String.format("方向:%f", cdata.Direct));
tvSpeed.setText(String.format("速度:%f", cdata.Speed));
tvGpsTime.setText(String.format("GPS时间:%s", cdata.GpsTime));
cdata.InfoType = infotype;
switch (infotype)
{
case 1:
tvInfoType.setText("信息来源状态:手动获取更新");
break;
case 2:
tvInfoType.setText("信息来源状态:位置改变更新");
break;
/*
* case 3: tvInfoType.setText("信息来源状态:位置改变更新"); break;
*/
}
}
}
@Override
public void onClick(View v)
{
if (v.equals(btnmanual))
{
showInfo(getLastPosition(), 1);
}
if (v.equals(btnsettimespace))
{
if (TextUtils.isEmpty(etSetTimeSpace.getText().toString()))
{
Toast.makeText(this, "请输入更新时间间隔", Toast.LENGTH_LONG).show();
etSetTimeSpace.requestFocus();
return;
}
int timespace = Integer.valueOf(etSetTimeSpace.getText().toString()) * 1000;
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
lm.requestLocationUpdates(provider, timespace, 10, locationListener);
}
if (v.equals(btnexit))
android.os.Process.killProcess(android.os.Process.myPid());
}
@Override
protected void onDestroy()
{
super.onDestroy();
}
}
android gps 获取经纬度
4星 · 超过85%的资源 需积分: 13 70 浏览量
2014-05-31
16:26:01
上传
评论
收藏 59KB RAR 举报
moheng
- 粉丝: 3
- 资源: 13
最新资源
- [信息办公]电信计费系统完整代码_netctossconformity.rar
- [其他类别]phplib v7.4a_phplib-7.4a.rar
- [上传下载]PHP下载系统 可按拼音生成下载页面的程序_dir.rar
- C语言-扫雷代码,VS2022,多文件形式
- matlab字母识别源程序,能够对26个字母进行识别,并以A为例做识别演示.rar
- 多目标规划优化问题 Matlab中常用于求解多目标达到问题的函数为fgoalattain.zip
- [影音娱乐]522QQ在线电视直播程序 v1.1_idcfree.rar
- 曼波整站系统(Mambors) v5.4.0_mambors5.4.0_CMS程序开发模板(使用说明+源代码+html).zip
- [其他类别]冰翎email发送类 v1.0_email.rar
- 【kk梦空间】仿淘宝海抢购页面1.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈