GeocoderNominatim.getFromLocationName()给我IOException

时间:2015-05-27 01:20:30

标签: android geocoding osmdroid

我正在使用OSMDroid v4.3和OSMBonuspack v5.1。

我通过国名'玻利维亚' to getLocationFromName()但我在行import UIKit class ViewController: UIViewController { @IBOutlet weak var memberChoice: UISegmentedControl! @IBOutlet weak var webView: UIWebView! @IBAction func getMember(sender:AnyObject){ var member: String = memberChoice.titleForSegmentAtIndex(memberChoice.selectedSegmentIndex)! var imageURLString: String! if member == "Daddy" { imageURLString = "file: ///Users/natashamays/Desktop/Daddy.jpg" } else if member == "Tasha" { imageURLString = "https://lh3.googleusercontent.com/-2c_GDVdcAFk/UBR7auHIHrI/AAAAAAAAEW8/gJ3F-MVUpL4/w140-h139-p/3.jpg" } else if member == "Jasmin" { imageURLString = "file:///Users/natashamays/Desktop/IMG_3879.JPG" } var imageURL: NSURL = NSURL(string:imageURLString)! webView.loadRequest(NSURLRequest(URL: imageURL)) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }

中得到一个IOException

互联网连接可用&在Manifest(android.permission.INTERNET)中设置

List<Address> geoResults = coderNominatim.getFromLocationName("Bolivia", 1);

我的LogCat输出如下:

GeocoderNominatim coderNominatim = new GeocoderNominatim(context);
try {
    List<Address> geoResults = coderNominatim.getFromLocationName("Bolivia", 1);

    if (geoResults.size() == 0) { 
             Toast.makeText(context, "Country not found.", Toast.LENGTH_SHORT).show();
     } else {
            Address address = geoResults.get(0);
            Bundle extras = address.getExtras();
            BoundingBoxE6 bb = extras.getParcelable("boundingbox");
            mapView.zoomToBoundingBox(bb);
     }
} catch (IOException e) {
    Toast.makeText(context, "Geocoding error", Toast.LENGTH_SHORT).show();
}

当我从LogCat(http://nominatim.openstreetmap.org/search?format=json&accept-language=en&addressdetails=1&limit=1&q=Bolivia)输入网址到我的浏览器时,我得到一个正确的JSON!

有任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

NetworkOnMainThreadException =&gt;请参阅important note of Tutorial_0

你有3个解决方案:

  • 使用AsyncTask
  • Android SDK&lt; 3.0.2
  • 或更改StrictMode.ThreadPolicy