- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我整天都在为此工作,我快要失去理智了。我正在尝试从谷歌的位置 api 获取坐标,以便从另一个 api (forcast.io) 获取天气。我的代码已经达到这样的程度,即 google api 正在使用正确的坐标完美地设置我的 TextViews ...但是当我尝试将这些坐标分配给将被获取并作为 http 链接发送到 forcast.io 的值时... lat 和 long 的值为 0.0。我不知道这笔交易是什么。在 onConnected() 方法中初始化它们后,我尝试从我的 TextViews 中获取文本作为字符串,我尝试在 onConnected() 方法中将值初始化为 double ...我已经完成了我所做的一切可以想到。这是我的代码:
package com.gardnerwebideas.myweather;
import android.content.Context;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
public class MainActivity extends AppCompatActivity
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = MainActivity.class.getSimpleName();
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private double mLatitude;
private double mLongitude;
private TextView mLatitudeText;
private TextView mLongitudeText;
//----------------------GOOGLE API BUILD----------------------------
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
//--------------------ON CREATE METHOD--------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLatitudeText = (TextView) findViewById(R.id.latitudeText);
mLongitudeText = (TextView) findViewById(R.id.longitudeText);
buildGoogleApiClient();
mGoogleApiClient.connect();
//---------------------CODE FROM ANDROID PROJECT-----------------------
//----------------SETTING UP NETWORK AND WEATHER API-------------------
String APIKey = "71e3dbfb4ebeb4dc3bcfb07097bb3645";
String forecastUrl = "https://api.forecast.io/forecast/"
+ APIKey + "/"
+ mLatitude
+ ","
+ mLongitude;
if (isNetworkAvailable()) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecastUrl)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
try {
if (response.isSuccessful()) {
Log.v(TAG, response.body().string());
} else {
alertUserAboutError();
}
} catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
}
}
});
} else {
Toast.makeText(this, "Network Unavailable", Toast.LENGTH_LONG).show();
}
Log.d(TAG, "Main UI code is running!");
}
//------------------------NETWORK METHODS-------------------------------
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
private void alertUserAboutError() {
AlertDialogFragment dialog = new AlertDialogFragment();
dialog.show(getFragmentManager(), "error_dialogue");
}
//------------------------GOOGLE API CODE-------------------------------
@Override
public void onConnected(Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
mLatitude = mLastLocation.getLatitude();
mLongitude = mLastLocation.getLongitude();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
最佳答案
您遗漏了一个关键点:地理位置信息以异步方式处理。在您的代码中,这意味着有关您的地理位置的信息将在您调用 forecast.io 之后 可用,这就是为什么您总是看到 0 值的原因。
只需按照以下方式更新您的代码(查看方法 callForecastAPI
):
import android.content.Context;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.example.ziby.testing.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
public class MainActivity extends AppCompatActivity
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = MainActivity.class.getSimpleName();
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private double mLatitude;
private double mLongitude;
private TextView mLatitudeText;
private TextView mLongitudeText;
//----------------------GOOGLE API BUILD----------------------------
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
//--------------------ON CREATE METHOD--------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLatitudeText = (TextView) findViewById(R.id.latitudeText);
mLongitudeText = (TextView) findViewById(R.id.longitudeText);
buildGoogleApiClient();
mGoogleApiClient.connect();
Log.d(TAG, "Main UI code is running!");
}
private void callForecastAPI(){
String APIKey = "71e3dbfb4ebeb4dc3bcfb07097bb3645";
String forecastUrl = "https://api.forecast.io/forecast/"
+ APIKey + "/"
+ mLatitude
+ ","
+ mLongitude;
if (isNetworkAvailable()) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecastUrl)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
try {
if (response.isSuccessful()) {
Log.v(TAG, response.body().string());
} else {
alertUserAboutError();
}
} catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
}
}
});
} else {
Toast.makeText(this, "Network Unavailable", Toast.LENGTH_LONG).show();
}
}
//------------------------NETWORK METHODS-------------------------------
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
private void alertUserAboutError() {
AlertDialogFragment dialog = new AlertDialogFragment();
dialog.show(getFragmentManager(), "error_dialogue");
}
//------------------------GOOGLE API CODE-------------------------------
@Override
public void onConnected(Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
mLatitude = mLastLocation.getLatitude();
mLongitude = mLastLocation.getLongitude();
callForecastAPI(); //call the forecast API when you get the location
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
关于java - 沮丧.. lastLocation.getLatitude() 将设置文本但不会启动双重。为此使用 google api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33224569/
我刚刚更新了我的位置 API 以使用 FusedLocationProviderClient 但我遇到了这个问题,当我关闭和打开 GPS 时,我总是得到空位置: val fusedLocationPr
所以我整天都在为此工作,我快要失去理智了。我正在尝试从谷歌的位置 api 获取坐标,以便从另一个 api (forcast.io) 获取天气。我的代码已经达到这样的程度,即 google api 正在
我是一名优秀的程序员,十分优秀!