【发布时间】:2010-12-09 03:36:35
【问题描述】:
我正在开发一个应用程序,该应用程序将为学生班车提供路线。我成功构建了这个应用程序,它将显示路线图以及停靠点。我现在想添加新功能,实时显示学生班车的位置。当学生查看班车路线时,他们还应该在地图上看到一个移动的图标,这是班车的实时位置。知道如何做到这一点...???
提前致谢
【问题讨论】:
我正在开发一个应用程序,该应用程序将为学生班车提供路线。我成功构建了这个应用程序,它将显示路线图以及停靠点。我现在想添加新功能,实时显示学生班车的位置。当学生查看班车路线时,他们还应该在地图上看到一个移动的图标,这是班车的实时位置。知道如何做到这一点...???
提前致谢
【问题讨论】:
首先你必须处理 GPS 以及如何在你的应用程序中使用它,为此,stackoverflow.com 已经有了一些答案:What is the simplest and most robust way to get the user's current location in Android?
那么您必须处理 google maps api 才能显示图标并进行地理编码:using-google-maps-android
更新:
public class JsonDownloader{
private static final String URI = "http://mySite.net/rest/getData.php";
@SuppressWarnings("unchecked")
public static String receiveData(){
String result = "";
DefaultHttpClient client = new DefaultHttpClient();
HttpGet method = new HttpGet(url);
HttpResponse res = null;
try {
res = client.execute(method);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
InputStream is = res.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
return result;
}
}
此代码将发出一个基本的 http 请求,并将您的 php 脚本返回的任何数据作为字符串返回。这对于只下载 gps 数据而不发布任何内容很有用。
要存储 gps 数据,您需要相同的类,您可以修改上面的类以执行 post 和 get 请求。所以首先你需要指定post参数。所以我们可以只添加方法postData:http://www.androidsnippets.org/snippets/36/index.html
所有这些都必须以异步方式发生,因为这是一个耗时的操作,有关详细信息,请查看 cmets 中的代码:http://android-projects.de/2010/08/13/threading-in-android-apps/
【讨论】: