2012-05-24 57 views
0

GPS獲取用戶的位置我wantto來到這裏與GPS用戶的位置是我的代碼麻煩通過從HTC野火S

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     t=(TextView)findViewById(R.id.textView1); 


     LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     LocationListener mlocListener = new MyLocationListener(); 

     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 

    } 

    public class MyLocationListener implements LocationListener 
    { 

     @Override 
     public void onLocationChanged(Location location) { 
      // TODO Auto-generated method stub 
      double lat=location.getLatitude(); 
      double longt= location.getLongitude(); 


      Geocoder gc=new Geocoder(getApplicationContext(),Locale.getDefault()); 
       try 
       { 
        List <Address> adresses=gc.getFromLocation(lat, longt, 1); 
        StringBuilder sb=new StringBuilder(); 
        if(adresses.size()>0) 
        { 
         Address address=adresses.get(0); 
         for(int i=0;i<address.getMaxAddressLineIndex();i++) 
          sb.append(address.getAddressLine(i)).append("\n"); 

         sb.append(address.getLocality()).append("\n"); 
         sb.append(address.getPostalCode()).append("\n"); 
         sb.append(address.getCountryName()); 

        } 

        String a="Latitude:"+lat+"\nLongtitude:"+longt+"\nAddress:"+sb.toString(); 
        t.setText(a); 
       } 

       catch(Exception e) 
       { 
        t.setText("Error.\n"+e.toString()); 
       } 
     } 

,我給了所有權限;

平臺就是Android 2.2.3和設備是HTC野火S

代碼不會引發一個錯誤,但它不是工作要麼

有沒有人有任何想法?

+0

你能定義「不工作」?您是否等待足夠長的GPS定位?不會改變嗎?它怎麼不起作用? – Sam

+0

噸不會改變,日誌貓什麼都不顯示,我等待20-30分鐘的GPS ...設備頂部的GPS圖標閃爍,並顯示設備可以獲得GPS信號 – dextererer

回答

0

您是否已通過免費的應用程序或任何軟件確認您應該收到GPS位置?有時候,我不能在30分鐘,60分鐘,2小時內獲得定位,即使我之前在同一位置獲得了定位。我在哪裏從來沒有在室內得到修復;但是如果我在室外走動然後回到室內,我會得到解決。

你的代碼在我的HTC上執行得很好(走出室外後)。雖然這並不影響現在什麼,我會建議改變:

LocationListener mlocListener = new MyLocationListener(); 

到:

MyLocationListener mlocListener = new MyLocationListener(); 

爲您做出MyLocationListener更加複雜。