实例介绍
【实例简介】进行GPS定位
【实例截图】
【核心代码】package ayh.mygps;
import java.util.Date;
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 myLocationManager;
private Location location;
private Criteria criteria;
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;
private DBGps dbgps = new DBGps(this);
/** 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);
dbgps.openDB();
initLocation();
}
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()
{
myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (myLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);// 高精度
criteria.setAltitudeRequired(true);// 显示海拔
criteria.setBearingRequired(true);// 显示方向
criteria.setSpeedRequired(true);// 显示速度
criteria.setCostAllowed(false);// 不允许有花费
criteria.setPowerRequirement(Criteria.POWER_LOW);// 低功耗
provider = myLocationManager.getBestProvider(criteria, true);
// 位置变化监听,默认5秒一次,距离10米以上
myLocationManager.requestLocationUpdates(provider, 5000, 10, locationListener);
} else
showInfo(null, -1);
}
private gpsdata getLastPosition()
{
gpsdata result = new gpsdata();
location = myLocationManager.getLastKnownLocation(provider);
if (location != null)
{
result.Latitude = (int) (location.getLatitude() * 1E6);
result.Longitude = (int) (location.getLongitude() * 1E6);
result.High = location.getAltitude();
result.Direct = location.getBearing();
result.Speed = location.getSpeed();
Date d = new Date();
d.setTime(location.getTime() 28800000);// 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("纬度:%d", cdata.Latitude));
tvLongitude.setText(String.format("经度:%d", 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;
*/
}
dbgps.addGpsData(cdata);
}
}
@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 (myLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
myLocationManager.requestLocationUpdates(provider, timespace, 10, locationListener);
}
if (v.equals(btnexit))
android.os.Process.killProcess(android.os.Process.myPid());
}
@Override
protected void onDestroy()
{
if (dbgps != null)
{
dbgps.closeDB();
dbgps = null;
}
super.onDestroy();
}
}
【实例截图】
【核心代码】package ayh.mygps;
import java.util.Date;
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 myLocationManager;
private Location location;
private Criteria criteria;
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;
private DBGps dbgps = new DBGps(this);
/** 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);
dbgps.openDB();
initLocation();
}
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()
{
myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (myLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);// 高精度
criteria.setAltitudeRequired(true);// 显示海拔
criteria.setBearingRequired(true);// 显示方向
criteria.setSpeedRequired(true);// 显示速度
criteria.setCostAllowed(false);// 不允许有花费
criteria.setPowerRequirement(Criteria.POWER_LOW);// 低功耗
provider = myLocationManager.getBestProvider(criteria, true);
// 位置变化监听,默认5秒一次,距离10米以上
myLocationManager.requestLocationUpdates(provider, 5000, 10, locationListener);
} else
showInfo(null, -1);
}
private gpsdata getLastPosition()
{
gpsdata result = new gpsdata();
location = myLocationManager.getLastKnownLocation(provider);
if (location != null)
{
result.Latitude = (int) (location.getLatitude() * 1E6);
result.Longitude = (int) (location.getLongitude() * 1E6);
result.High = location.getAltitude();
result.Direct = location.getBearing();
result.Speed = location.getSpeed();
Date d = new Date();
d.setTime(location.getTime() 28800000);// 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("纬度:%d", cdata.Latitude));
tvLongitude.setText(String.format("经度:%d", 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;
*/
}
dbgps.addGpsData(cdata);
}
}
@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 (myLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
myLocationManager.requestLocationUpdates(provider, timespace, 10, locationListener);
}
if (v.equals(btnexit))
android.os.Process.killProcess(android.os.Process.myPid());
}
@Override
protected void onDestroy()
{
if (dbgps != null)
{
dbgps.closeDB();
dbgps = null;
}
super.onDestroy();
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论