3

I am working on an app which will provide route for the student shuttle. I successfully built this app which will show route map along with stops. I now like to add new functionality which will show the location of student shuttle in real time. When student looks at the shuttle route, they should also see a moveining icon on the map which is the real time location of the shuttle. ANy idea how this can be done ...???

Thanks in advance

bp581
  • 859
  • 1
  • 16
  • 47

1 Answers1

1

Well first you have to deal with GPS and how to use it in you application, for that purpose stackoverflow.com has some answers already: What is the simplest and most robust way to get the user's current location in Android?

Then your have to deal with the google maps api in order to display the icons and to do geo coding: using-google-maps-android

Update:

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;
    }
} 

This code would make a basic http request and return the any data that is returned by your php script as string. This is useful for just downloading gps data without posting anything.

For storing gps data you need the same class, you could modify the class above to perform both post and get requests. So first you need to specify the post parameters. So we can add just the method postData: http://www.androidsnippets.org/snippets/36/index.html

All this has to happen in a async way because it's a time consuming operation, for details have a look at the code in the comments: http://android-projects.de/2010/08/13/threading-in-android-apps/

Community
  • 1
  • 1
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • Thanks for the speedy response. I am quite comfertable in getting the user location. But in order to make the student see the real time information of the shuttle what needs to be done. Here is what I was planning on doing. I will create a separate app which the shuttle driver will use. This app will continusly send the coordinate location of the shuttle driver to some server where I can store the information. When the student wants to know the real time information of the shuttle, I will retrieve the stored coordinate and show it to student. Is there any other method ..??? – bp581 Dec 09 '10 at 05:06
  • well you need a API for you method. The shuttle app stores the data online and you can access it for any purpose. You need some async logic for it too. Here are some resources: http://stackoverflow.com/questions/3846688/android-app-using-data-from-internet-resource/3848958#3848958 http://stackoverflow.com/questions/3778676/design-approach-android-and-web-service/3779102#3779102 http://stackoverflow.com/questions/3745405/threading-ui-updates-in-android/3745423#3745423 It is a very interdisciplinary topic that you are questioning – DarkLeafyGreen Dec 09 '10 at 06:10
  • Thanks..I looked into the links..thay are great. Could you please give me a good link/method to write data to database located in remote server. I can then plan on exposing the data in JSON format ...!!! – bp581 Dec 09 '10 at 14:30
  • Well e.g. you are using php on your server, then you could make a http request appending the gps data to the url, like mysite.net/restapi/storeGPS.php?lng=xxxxx&lat=xxxxxx. the php script would then store the gps data in database. here is an example http://www.helloandroid.com/tutorials/connecting-mysql-database – DarkLeafyGreen Dec 09 '10 at 15:05
  • Refering to the link, what I can do is in the code HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://example.com/StoreGPS.php?lng=xxxxx&lt=yyyyyyyyy"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); Am I right ...?? Thanks again for timely response..appreciate your response.... – bp581 Dec 11 '10 at 04:23
  • what do you mean with "what can I do"? I have updated my answer with an example – DarkLeafyGreen Dec 11 '10 at 09:00