首先,您需要处理Google Maps,因为您将显示一个地图视图。请查看在mobiForge上使用Android中的Google Maps。
其次,您需要一个提供GPS数据的类。使用消息处理程序获取位置数据并更新UI很简单。这是一个示例:
public MyGPS implements LocationListener{
public LocationManager lm = null;
private MainActivity SystemService = null;
private double mLongitude = 0;
private double mLatitude = 0;
public MyGPS(MainActivity sservice){
this.SystemService = sservice;
this.startLocationService();
}
public void startLocationService(){
this.lm = (LocationManager) this.SystemService.getSystemService(Context.LOCATION_SERVICE);
this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, this);
}
public void onLocationChanged(Location location) {
location = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
try {
this.mLongitude = location.getLongitude();
this.mLatitude = location.getLatitude();
} catch (NullPointerException e) {
Log.i("Null pointer exception " + mLongitude + "," + mLatitude, null);
}
}
}
在您的onCreate方法中,创建此类的一个实例,位置监听器将开始监听GPS更新。但是,由于您不知道从活动中它们是否已设置或为空,因此无法访问lng和lat。因此,您需要一个处理程序,在设置lat和lng时向您的主活动发送消息:
在以下方法中进行修改:
public void onLocationChanged(Location location) {
location = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
try {
this.mLongitude = location.getLongitude();
this.mLatitude = location.getLatitude();
Message msg = Message.obtain();
msg.what = UPDATE_LOCATION;
this.SystemService.myViewUpdateHandler.sendMessage(msg);
} catch (NullPointerException e) {
Log.i("Null pointer exception " + mLongitude + "," + mLatitude, null);
}
}
在你的主活动中添加以下内容:
Handler myViewUpdateHandler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_LOCATION:
}));
}
super.handleMessage(msg);
}
};
由于处理程序位于您的MapActivity中,因此您可以在处理程序本身轻松更新UI。每次可用GPS数据时,都会触发消息并由处理程序接收。
开发REST API是一件非常有趣的事情。一种简单的方法是在Web服务器上拥有一个php脚本,该脚本在请求时返回一些json数据。如果您想开发这样的服务,则此教程可能会对您有所帮助,link。