2013-06-16 2 views
2

J'ai une application avec Google Maps avec une seule activité où une carte est affichée. J'ai un menu qui me permet de changer le type de carte mais je voudrais avoir une option pour obtenir ma position actuelle.Android: impossible d'obtenir l'emplacement actuel sur les cartes

Voici le code de mon activité:

package com.example.chiapa_mapas; 

import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.GoogleMap.OnMapClickListener; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

import android.content.Context; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.MenuInflater; 
import android.view.MenuItem; 

public class MainActivity extends android.support.v4.app.FragmentActivity implements OnMapClickListener, LocationListener{ 

    private GoogleMap mMap; 
    private Context ctx; 
    private LocationManager locationManager; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     mMap.setOnMapClickListener(this); 


    // Enable LocationLayer of Google Map 
     mMap.setMyLocationEnabled(true); 
     // Getting LocationManager object from System Service LOCATION_SERVICE 
     locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 


     } 

    @Override 
    public boolean onCreateOptionsMenu(android.view.Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.layout.opcoes, menu); 
     return true; } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 

     case R.id.normal: 
      mMap.setMapType(1); 
      break; 

     case R.id.satellite: 
      mMap.setMapType(2); 
      break; 

     case R.id.terrain: 
      mMap.setMapType(3); 
      break; 
     case R.id.hybrid: 
      mMap.setMapType(4); 
      break; 

     case R.id.none: 
      mMap.setMapType(0); 
      break; 

     case R.id.posicao_actual: 
      //mMap.setMapType(0); 
      Criteria criteria = new Criteria(); 
      // Getting the name of the best provider 
      String provider = locationManager.getBestProvider(criteria,true); 
      // Getting Current Location 
      Location location = locationManager.getLastKnownLocation(provider); 
      if (location != null) { 
       onLocationChanged(location); 
       } 

      locationManager.requestLocationUpdates(provider, 20000, 0,(android.location.LocationListener) ctx); 

      break; 

     default: return super.onOptionsItemSelected(item); 
     } 

     return true; 
    } 

    @Override 
    public void onMapClick(LatLng position) { 
     // TODO Auto-generated method stub 

     mMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))); 

     //double ltt = position.latitude; 
     //double lgt = position.longitude; 

     //String msg = "Latitude: " + Double.toString(ltt) + " , Longitude: " + Double.toString(lgt) + ""; 

     //Toast toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT); 
     //toast.show();  
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 

     // Getting latitude of the current location 
     double latitude = location.getLatitude(); 
     // Getting longitude of the current location 
     double longitude = location.getLongitude(); 
     // Creating a LatLng object for the current location 
     LatLng latLng = new LatLng(latitude, longitude); 
     // Showing the current location in Google Map 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     // Zoom in the Google Map 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 
     // Setting latitude and longitude in the TextView 

     locationManager.removeUpdates((android.location.LocationListener) this); 

    } 

} 

Quelqu'un peut-il aider? Lorsque j'appuie sur l'option "posicao actuel" (emplacement actuel) il se bloque. Merci à l'avance Chiapa

Répondre

0

changement

locationManager.requestLocationUpdates(provider, 20000, 0,(android.location.LocationListener) ctx); 

à

locationManager.requestLocationUpdates(provider, 20000, 0,this); 

et supprimer les mises à jour trop

+0

Merci pour votre réponse, mais il se bloque toujours: J'inséré votre code, mais je devais lancer le « cette » partie il est comme ça maintenant: locationManager.requestLocationUpdates (fournisseur, 20000, 0, (android.location.LocationListener) ce); de toute façon, il se bloque encore et je ne sais pas quoi faire – chiapa

+0

Donc, j'ajouté cette mise en œuvre et il fonctionne maintenant: MainActivity public class android.support.v4.app.FragmentActivity implémente LocationListener, android.location. LocationListener, OnMapClickListener { merci encore pour votre problème – chiapa

4

Cela fonctionne pour moi:

public void methodName(){ 
     LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
     android.location.LocationListener locationListener = new android.location.LocationListener() { 
      public void onLocationChanged(Location location) { 
       //Any method here 
      } 
      public void onStatusChanged (String provider, int status, Bundle extras){} 
      public void onProviderEnabled(String provider) {} 
      public void onProviderDisabled (String provider){} 
     }; 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, locationListener); 
    } 

Merci chiapa.

+0

Merci ** vous **, je vote votre réponse! – chiapa