【问题标题】:Android Map tracking and moving an iconAndroid 地图跟踪和移动图标
【发布时间】:2010-12-09 03:36:35
【问题描述】:

我正在开发一个应用程序,该应用程序将为学生班车提供路线。我成功构建了这个应用程序,它将显示路线图以及停靠点。我现在想添加新功能,实时显示学生班车的位置。当学生查看班车路线时,他们还应该在地图上看到一个移动的图标,这是班车的实时位置。知道如何做到这一点...???

提前致谢

【问题讨论】:

    标签: android map


    【解决方案1】:

    首先你必须处理 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/

    【讨论】:

    • 感谢您的快速响应。我很乐意获得用户位置。但是为了让学生看到班车的实时信息需要做什么。这是我打算做的事情。我将创建一个单独的应用程序供班车司机使用。这个应用程序会不断地将穿梭司机的坐标位置发送到我可以存储信息的某个服务器。当学生想知道班车的实时信息时,我会检索存储的坐标并显示给学生。还有其他方法吗..???
    • 那么你需要一个 API 为你的方法。穿梭应用程序在线存储数据,您可以出于任何目的访问它。你也需要一些异步逻辑。这里有一些资源:stackoverflow.com/questions/3846688/…stackoverflow.com/questions/3778676/…stackoverflow.com/questions/3745405/…这是一个非常跨学科的话题,你正在质疑
    • 谢谢..我查看了链接..他们很棒。您能否给我一个很好的链接/方法来将数据写入位于远程服务器中的数据库。然后我可以计划以 JSON 格式公开数据......!!!
    • 好吧,例如您在服务器上使用 php,然后您可以发出 http 请求,将 gps 数据附加到 url,例如 mysite.net/restapi/storeGPS.php?lng=xxxxx&lat=xxxxxx。然后 php 脚本会将 gps 数据存储在数据库中。这是一个例子helloandroid.com/tutorials/connecting-mysql-database
    • 参考链接,我能做的是在代码中 HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("example.com/StoreGPS.php?lng=xxxxx&lt=yyyyyyyyy"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); 我说的对吗...?? 再次感谢您的及时回复..感谢您回应....
    猜你喜欢
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多