2012-05-24 58 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更加复杂。