Google maps - location using GPS android

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I am trying to determine the current geolocation, i ve used the following code but i m not getting any thing so far  :

public class Activity_map extends FragmentActivity implements LocationListener {
public  double latituteField;
public  double longitudeField;
  private GoogleMap map;
  private LocationManager locationManager;
  LatLng pos;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gmap);
 loc   ationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
     Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);
    if (location != null) {
      System.out.println("Provider " + provider + " has been selected.");
      onLocationChanged(location);
      map.addMarker(new MarkerOptions().position(pos).title("MY POSITION").snippet("GPS")
                .draggable(true));
    } else {
      latituteField=0;
      longitudeField=0;
      }
     FragmentManager myFragmentManager = getSupportFragmentManager();
    SupportMapFragment myMapFragment 
       = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);      

    map = myMapFragment.getMap();
    }

  @Override
  protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(

            LocationManager.GPS_PROVIDER,5000,10,this);
      }
      @Override
  protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
      }

  public void onLocationChanged(Location location) {
    double lat = (double) (location.getLatitude());
    double lng = (double) (location.getLongitude());
   pos= new LatLng(lat,lng);


  }

  public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

  }

  public void onProviderEnabled(String provider) {
      Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();

  }

  public void onProviderDisabled(String provider) {
      Toast.makeText(this, "Disabled provider " + provider,
                Toast.LENGTH_SHORT).show();
  }
      }

The problem is that i didn t get any marker.. i tried to do that but i had more than one marker

public void onLocationChanged(Location location) {
    double lat = (double) (location.getLatitude());
    double lng = (double) (location.getLongitude());
        pos=new LatLng(lat,lng);
          map.addMarker(new MarkerOptions().position(pos).title("MY POSITION").snippet("GPS")
            .draggable(true));
       }

any help will be appreciated, thank you

Answers

You havent specified any image for marker..So add it like this..

map.addMarker(new MarkerOptions().position(pos).title("MY POSITION").snippet("GPS").icon(BitmapDescriptorFactory.fromResource(R.drawable.yourmarkericon)));
  and    

Remove your marker from onCreate() as it is called only once at the time of activity creation..

Add your marker inside onLocationChanged() or add marker in a function and call that function inside onLocationChanged()

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/23224959/location-using-gps-android

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils