Select to view content in your preferred language

Location Service (GPS)

2280
5
01-04-2012 05:47 PM
deleted-user-K_IRAXrpGKsG
Deactivated User
I need to find the user location with the phone's GPS. I know that the LocationService class is used but I'm not sure how. My map works fine but I do not know how to put that little blue circle on the map with the user location. Below is my code:
public class HelloWorld extends Activity {
 MapView map = null;


 /** Called when the activity is first created. */
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  // Retrieve the map and initial extent from XML layout
  map = (MapView)findViewById(R.id.map);
  // Add dynamic layer to MapView
  map.addLayer(new ArcGISDynamicMapServiceLayer("" +
    "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"));
  
  //Retrieve the non-configuration instance data that was previously returned. 
  Object init = getLastNonConfigurationInstance();
  if (init != null) {
   map.restoreState((String) init);
   
   
   LocationService ls = map.getLocationService();
   ls.setAccuracyCircleOn(true);
   ls.start();
  } 
 }
 

 protected void onPause() {
  super.onPause();
  map.pause();
 }

 protected void onResume() {
  super.onResume(); 
  map.unpause();
 } 
 
}
0 Kudos
5 Replies
ArchanaAgarwal
Occasional Contributor
Please take a look at the location service sample. That should help you. Also what you are doing should work. Are you seeing any errors?
0 Kudos
deleted-user-K_IRAXrpGKsG
Deactivated User
I got it to work with the menu but need it to toggle on and off. Any ideas?

Here's the code that works, I would just like it to turn on when the menu button is clicked and turn off again if the user doesn't need it.
  

                                        ls = map.getLocationService(); 
            ls.start();
            ls.setAccuracyCircleOn(true); 
0 Kudos
KyleShimabukuro
Occasional Contributor
You could add an if statement to your menu button:

LocationService ls = map.getLocationService();
if (ls.isStarted() == false) { 
  ls.start();
}
else {
  ls.stop();
};
0 Kudos
deleted-user-K_IRAXrpGKsG
Deactivated User
Thanks Kyle! I am very new to Java so I was trying to write an if statement without the == false.
0 Kudos
JohnAllen
Deactivated User
You could add an if statement to your menu button:

LocationService ls = map.getLocationService();
if (ls.isStarted() == false) { 
  ls.start();
}
else {
  ls.stop();
};


I used this and it works great. Thanks!
I have one question. When I click my button to turn back on the GPS, the blue dot changes to grey, but it still works properly. How to I keep the blue dot blue?

Here is my code:

//Setting up the button reference
          ZoomToMyLocation = (Button) findViewById (R.id.button1);
              
          //When "Button ZoomToMyLocation" is clicked, method "public void onClick(View v)" gets GPS location. 
          ZoomToMyLocation.setOnClickListener(new View.OnClickListener() {
     
            public void onClick(View v) {
          LocationService ls = map.getLocationService();
     ls = map.getLocationService();
     
     if (ls.isStarted() == false){
      ls.start();
     }
     
     else {
      ls.stop();
     };
     
     ls.setAccuracyCircleOn(true);
   
         }
          
          });
0 Kudos