6

I want to display my current location, but it shows me the latitude and longitude that I set on the command line! If I don't set the parameters on the command line, it shows me the latitude and longitude included in Eclipse!

Is there any one who has an idea about that? I'm testing with AVD.

I made a test to see if my location is enabled, but it isn't. I'm not sure why that is the case.

This is my code:

package com.manita.mapuse;


import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;



 public class MapuseActivity extends MapActivity implements LocationListener {

     private MapView mapView = null;
        private LocationManager lm = null;
        private double lat = 0;
        private double lng = 0;
        private double alt = 0;
        private MapController mc = null;
        private MyLocationOverlay myLocation = null;
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mapView = (MapView) this.findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);

        lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this);
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0,
            this);

        mc = mapView.getController();
        mc.setZoom(10);

        myLocation = new MyLocationOverlay(getApplicationContext(), mapView);
        myLocation.runOnFirstFix(new Runnable() {
            public void run() {
            mc.animateTo(myLocation.getMyLocation());
            mc.setZoom(10);
            }
        });


         LocationManager locationManager;
         String context = Context.LOCATION_SERVICE; 
         locationManager = (LocationManager)getSystemService(context);
         String provider = LocationManager.GPS_PROVIDER;
         lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this);
         Location location = locationManager.getLastKnownLocation(provider);
         locationManager.getProvider(provider);
        if(!locationManager.isProviderEnabled(provider)){
             locationManager.setTestProviderEnabled(provider, true);
            }
            boolean enabled = locationManager.isProviderEnabled(provider);
            if(enabled){

                Toast.makeText(getBaseContext(),
                         "provide enabled : Latitude = " + lat + " Longitude = " + lng+" Altitude = " + alt,
                         Toast.LENGTH_SHORT).show();

            }
            else{
                Toast.makeText(getBaseContext(),
                         "provider disabled : Latitude = " + lat + " Longitude = " + lng+" Altitude = " + alt,
                         Toast.LENGTH_SHORT).show();

            }
            if(location!=null){
              lat = location.getLatitude();
              lng = location.getLongitude();
              alt = location.getAltitude();
              Toast.makeText(getBaseContext(),
                         "providerrr enabled : Latitude = " + lat + " Longitude = " + lng+" Altitude = " + alt,
                         Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(getBaseContext(),
                         "location not found : Latitude = " + lat + " Longitude = " + lng+" Altitude = " + alt,
                         Toast.LENGTH_SHORT).show();
            } 



        mapView.getOverlays().add(myLocation);
        if (myLocation.isMyLocationEnabled()!=false)
        {
            GeoPoint p =myLocation.getMyLocation();
            lat= p.getLatitudeE6();
            lng= p.getLongitudeE6();
            alt = location.getAltitude();
            Toast.makeText(getBaseContext(),
                     "geolocalisation activé lat: "+lat+ " long: " +lng +" Altitude = " + alt,
                     Toast.LENGTH_SHORT).show();
        }
        else
        {

            Toast.makeText(getBaseContext(),
                     "geolocalisation desactivée" ,
                     Toast.LENGTH_SHORT).show();
        }

        // appel de la fonction insert values 
        insertvalues(lat, lng);
        }

        @Override
        protected void onResume() {
        super.onResume();
        myLocation.enableMyLocation();
        myLocation.enableCompass();
        }

        @Override
        protected boolean isRouteDisplayed() {
        return false;
        }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_S) {
            mapView.setSatellite(!mapView.isSatellite());
            return true;
        }
        if (keyCode == KeyEvent.KEYCODE_T) {
            mapView.setSatellite(!mapView.isTraffic());
            return true;
        }
        return super.onKeyDown(keyCode, event);
        }

        @Override
        public void onLocationChanged(Location location) {
        lat = location.getLatitude();
        lng = location.getLongitude();
        Toast.makeText(
            getBaseContext(),
            "Location change to : Latitude = " + lat + " Longitude = "
                + lng, Toast.LENGTH_SHORT).show();
        GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
        mc.animateTo(p);
        mc.setCenter(p);
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }


        ///// insert valeurs ....
        public void insertvalues(double lat, double lng){




          //http post c à d envoi des données
          try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new   

              HttpPost("http://www.pizza-paris.com/clic/marwa/test/form.php?latitude="+lat+"&longitude="+lng);

                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                InputStream is = entity.getContent();
                Log.i("postData", response.getStatusLine().toString());

            }

            catch(Exception e)
            {
                Log.e("log_tag", "Error in http connection "+e.toString());
            }       


            }
 }

Thanks!

Zoot
  • 2,217
  • 4
  • 29
  • 47
manita marwa
  • 255
  • 1
  • 4
  • 13
  • gps won't work in an emulator. Try your code on a real device. – Snicolas Dec 23 '11 at 13:11
  • Please check this link it may help you. [GPS location in Android Emulator](http://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator) – Yugandhar Babu Dec 23 '11 at 13:25
  • thanks Snicolas mybe your answer is what i'm searching but how can i get the apk file??? – manita marwa Dec 23 '11 at 13:49
  • thanks Snicolas mybe your answer is what i'm searching thanks Yughandur too but can you tell me how can i install it on a real device? and can i test it with an iphone or htc???!! – manita marwa Dec 23 '11 at 13:52
  • Get an Android phone, enable USB debugging in the system settings, connect it via USB to your computer, and run your app in Eclipse -- it will give you the choice of starting your app in either a compatible emulator instance or the connected device. An Android app won't work with an iPhone, of course. – Philipp Reichart Dec 23 '11 at 14:30
  • "gps won't work in an emulator", "Get an Android Phone" but @YugandharBabu's question link in his comment shows that it's possible to use `geo fix` to set the current location coordinates of the Android emulator – SSH This May 07 '13 at 14:45

3 Answers3

4

Open your Virtual Devices manager and klick Edit. At the section Hardware click New and add GPS support and change the value to true. Now start the emulator and open a console. If the emulator is boot up complete type in the console telnet localhost 5554. When the telnet connection is established you can put in locations like this: geo fix 21.42 42

Thommy
  • 5,070
  • 2
  • 28
  • 51
3

To add to the existing answers, if you really need to test the functionality on your emulator you can use mock locations. More you can find here http://developer.android.com/guide/topics/location/obtaining-user-location.html#MockData.

Keep in mind that you have to add the required permission for this (ACCESS_MOCK_LOCATION).

Hope this helps!

Dimitris Makris
  • 5,183
  • 2
  • 34
  • 54
1

Unless your PC/Mac has GPS, AVD doesn't have ability to detect your location. Test your program with real device or by entering values manually.

Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
  • Or use `geo fix` http://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator – SSH This May 07 '13 at 14:46