0

// 在此我有时会获取位置,有时位置未检索到我已打开 gps 和 gprs 并添加了所需的权限。我得到了真正的问题是什么

包 com.example.addr;

import java.util.List;
import java.util.Locale;

 import android.location.Address;
 import android.location.Criteria;
 import android.location.Geocoder;
 import android.location.Location;
 import android.location.LocationListener;
 import android.location.LocationManager;
 import android.os.Bundle;
 import android.app.Activity;
 import android.content.Context;
 import android.view.Menu;
  import android.widget.TextView;


  public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LocationManager locationManager; 
    String context = Context.LOCATION_SERVICE; 
    locationManager = (LocationManager)getSystemService(context); 

    Criteria crta = new Criteria(); 
    crta.setAccuracy(Criteria.ACCURACY_FINE); 
    crta.setAltitudeRequired(false); 
    crta.setBearingRequired(false); 
    crta.setCostAllowed(true); 
    crta.setPowerRequirement(Criteria.POWER_LOW); 
    String provider = locationManager.getBestProvider(crta, true); 

 // String provider = LocationManager.GPS_PROVIDER; 
    Location location = locationManager.getLastKnownLocation(provider); 
    updateWithNewLocation(location); 

    locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); 
    } 
private final LocationListener locationListener = new LocationListener() 
{ 

@Override 
public void onLocationChanged(Location location) { 
updateWithNewLocation(location); 
} 

@Override 
public void onProviderDisabled(String provider) { 
updateWithNewLocation(null); 
} 

@Override 
public void onProviderEnabled(String provider) { 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
} 

}; 

private void updateWithNewLocation(Location location) { 
    String latLong;
    TextView myLocation; 
    myLocation = (TextView) findViewById(R.id.myLocation); 

    String addressString = "Sorry  No Address Found"; 

    if(location!=null) { 
        double lat = location.getLatitude(); 
    double lon = location.getLongitude(); 
    latLong = "Lat:" + lat + "\nLong:" + lon; 

    double lattitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 
    Geocoder gc = new Geocoder(this,Locale.getDefault()); 
    try { 
    List<Address> addresses= gc.getFromLocation(lattitude, longitude, 1); 
    StringBuilder sb = new StringBuilder(); 
    if(addresses.size()>0) { 
    Address address=addresses.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()); 
} 
    addressString = sb.toString(); 
    } 
    catch (Exception e) { 
    } 
    } else { 
    latLong = " NO Location Found "; 
    } 

    myLocation.setText("Current Position is :\n"+ latLong + "\n"+  addressString ); 
       }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


}
4

2 回答 2

0

我建议明确告诉 Android 你想要哪个位置提供程序。使用标准可以产生粗略的结果。Criteria.POWER_LOW 的设置可能会不考虑 GPS,因为 GPS 被认为是高功率消耗。

例如,对于 GPS 提供商:

String provider = LocationManager.GPS_PROVIDER; 
...
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); 

和网络提供商:

String provider = LocationManager.NETWORK_PROVIDER; 
...
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
于 2012-10-15T22:58:14.617 回答
0

GPS 将无法在屋檐下工作,也无法让您更好地尝试使用网络提供商。看到这个帖子很有帮助

于 2012-10-15T16:33:39.317 回答