2011-12-23 65 views
6

我想顯示我的當前位置,但它顯示我在命令行上設置的經度和緯度!如果我不在命令行上設置參數,它會顯示Eclipse中包含的經緯度!android gps當前位置

有沒有人對此有所瞭解?我正在用AVD進行測試。

我做了一個測試,看看我的位置是否啓用,但事實並非如此。我不確定爲什麼會這樣。

這是我的代碼:

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


      } 
} 

謝謝!

+0

gps無法在模擬器中工作。在真實設備上試用您的代碼。 – Snicolas

+0

請檢查這個鏈接它可能會幫助你。 [Android模擬器中的GPS位置](http://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator) –

+0

感謝Snicolas mybe你的回答就是我要搜索的內容但我怎麼才能得到apk文件? –

回答

1

除非您的PC/Mac具有GPS,否則AVD無法檢測到您的位置。用真實設備測試您的程序或手動輸入數值。

+0

非常感謝:) –

+0

或使用'geo fix' http://stackoverflow.com/questions/2279647/how-emulate-gps-location-in-the-android-emulator –

4

打開虛擬設備管理器並單擊編輯。在硬件部分單擊新建並添加GPS支持並將該值更改爲true。 現在啓動模擬器並打開一個控制檯。 如果仿真器在控制檯telnet localhost 5554中啓動完成類型。 當建立telnet連接時,您可以放置​​如下位置:geo fix 21.42 42

+0

是的,它得到的位置我在本地主機上設置,但我希望它顯示我當前的位置,這意味着我現在在哪裏! –

+0

找出你當前的座標並使用'geo fix'輸入它們或去購買一臺android。 –

3

要添加現有答案,如果您確實需要在模擬器上測試功能,則可以使用模擬位置。更多你可以在這裏找到http://developer.android.com/guide/topics/location/obtaining-user-location.html#MockData

請記住,您必須爲此添加所需權限(ACCESS_MOCK_LOCATION)。

希望這會有所幫助!

+0

是的,我做了,但我希望它顯示我的當前位置不是模擬器中的locatiob! –

+0

然後你需要一個智能手機與您的電腦與GPS或GPS。 –

+0

是的!我的應用程序的版本是7!我可以用iphone測試它嗎? –