【问题标题】:Get Weather Info with API for android使用适用于 Android 的 API 获取天气信息
【发布时间】:2012-02-24 19:47:37
【问题描述】:

当我有经度和纬度坐标时,我想使用谷歌天气 api 获取天气信息。所以我采用代码:

private void updateWeather(){
    new Thread(){
        public void run() {
            //get Weather start:
              LocationHelper helper = new LocationHelper(MomentsActivity.this);
              Location location = helper.getLocation();

              if(null != location){
                  String request = "http://www.google.com/ig/api?weather=,,,"+location.getLatitude()
                          +","+location.getLongitude();
                  XmlParserUtil parser = new XmlParserUtil(mHandler);
                  try {
                      parser.parse(HttpUtil.getWeather(request));
                  } catch (ClientProtocolException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
            //end
        };
    }.start();
}

但是当我想从LocationManager中获取经纬度时,发现位置一直为null。请查看获取位置的代码:

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;

public class LocationHelper {
    Context context;
    public LocationHelper(){}
    public LocationHelper(Context context){
        this.context = context;
    }
    public Location getLocation(){
        LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(null == location){
            location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }
        return location;
    }
}

请帮我找出错误。谢谢。

另外: 我添加了一些权限:

<uses-permission android:name="android.permission.INTERNET" ></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" ></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" ></uses-permission>

都一样,我打开了GPS和wifi,但位置也是空的。

【问题讨论】:

  • 您是否在清单文件中添加了所需的权限?见Link
  • 我添加了一些权限:

标签: android networking gps location weather


【解决方案1】:
     LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location provider.
                System.out.println("Location is: :" + location.toString());

            }

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

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}
        };

        // Register the listener with the Location Manager to receive location updates
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
//      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, locationListener);

你可以得到比这更花哨的东西。例如,连续运行位置代码应该会消耗大量电池。您还需要听取多个响应并过滤以在准确性和最近性之间进行优先级排序。祝你好运。

【讨论】:

    猜你喜欢
    • 2013-01-15
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    相关资源
    最近更新 更多