我有以下代码,基本上我想知道如何使用这个位置方法distanceBetween()来查找我的.JSON文件中500公里以外的对象。我试图将if语句和distanceBet之间的用法组合在一起,但它不能识别它,我知道我的语法有一些实际问题。这是尝试这样做的正确方法吗?非常感谢任何帮助或提示或代码!
package com.dredaydesigns.radiostationfinder;
import android.R;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.gson.JsonParser;
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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;
import java.io.IOException;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks {
// String HTTPRadioURL = "https://transition.fcc.gov/fcc-bin/fmq?state=&call=&city=&arn=&serv=FC&vac=3&freq=0.0&fre2=107.9&facid=&asrn=&class=&dkt=&list=0&dist=100&dlat2="
// + latitude + "&mlat2=&slat2=&NS=N&dlon2="
// + longitude +"&mlon2=&slon2=&EW=W&size=9";
public static final String TAG = MainActivity.class.getSimpleName();
private RadioData mRadioData;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
@Bind(R.id.longitudeLabel) TextView mLongitudeLabel;
@Bind(R.id.latitudeLabel) TextView mLatitudeLabel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_content);
ButterKnife.bind(this);
double latitude = 32;
double longitude = -96;
double latitudeStations;
double longitudeStations;
final RadioData[] mRadioData = new RadioData[1];
//String radioFinderURL = "http://data.fcc.gov/lpfmapi/rest/v1/lat/" + latitude + "/long/" + longitude + "?format=json&secondchannel=true";
//String HTTPRadioURL = "https://transition.fcc.gov/fcc-bin/fmq?state=&call=&city=&arn=&serv=FC&vac=3&freq=0.0&fre2=107.9&facid=&asrn=&class=&dkt=&list=0&dist=100&dlat2="
// + latitude + "&mlat2=&slat2=&NS=N&dlon2="
// + longitude +"&mlon2=&slon2=&EW=W&size=9";
String radioFinderURL = "http://dredaycreative.com/json/radioData.json";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(radioFinderURL)
.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 {
String jsonData = response.body().string();
Log.v(TAG,jsonData);
if (response.isSuccessful()) {
mRadioData[0] = getCurrentDetails(jsonData);
}
} catch (IOException e) {
Log.e(TAG, "Exception Caught: ", e);
}
catch(JSONException e){
Log.e(TAG, "Exception Caught:", e);
}
}
});
}
private RadioData getCurrentDetails(String jsonData) throws JSONException {
JSONObject radioData = new JSONObject(jsonData);
String callSign;
double latitudeStations;
double longitudeStations;
int frequency;
JSONArray jsonArray = radioData.getJSONArray("array");
for(int i =0; i<jsonArray.length(); i++){
callSign = radioData.getJSONObject(i+"")
.getJSONArray("array").getJSONObject(i).getString("FIELD1");
frequency = Integer.parseInt(radioData.getJSONObject(i + "")
.getJSONArray("array").getJSONObject(i).getString("FIELD2"));
longitudeStations = Double.parseDouble(radioData.getJSONObject(i+"")
.getJSONArray("array").getJSONObject(i).getString("FIELD20"));
latitudeStations = Double.parseDouble(radioData.getJSONObject(i+"")
.getJSONArray("array").getJSONObject(i).getString("FIELD24"));
Log.i(TAG, "From JSON: " + callSign + frequency + latitudeStations + longitudeStations);
if {
distanceBetween(double latitude, double longitude, double latitudeStations, double longitudeStations, float[] results) <100km
RadioData currently = new RadioData();
}
}
return new RadioData();
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
mLatitudeLabel.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeLabel.setText(String.valueOf(mLastLocation.getLongitude()));
}
}
@Override
public void onConnectionSuspended(int i) {
}
}
在PMCMT 8/17/15 7:55评论后更新了命令
好的,谢谢你的帮助!我以这种方式把它换了,我变暖了,哈?
private RadioData getCurrentDetails(String jsonData) throws JSONException {
JSONObject radioData = new JSONObject(jsonData);
static void distanceBetween;
String callSign;
double latitudeStations;
double longitudeStations;
int frequency;
private int mRadius = distanceBetween(double latitude, double longitude, double latitudeStations, double longitudeStations, float[] results)
callSign = radioData.getJSONObject(i + "")
.getJSONObject(i).getString("FIELD1");
frequency = Integer.parseInt(radioData.getJSONObject(i + "")
.getJSONObject(i).getString("FIELD2"));
longitudeStations = Double.parseDouble(radioData.getJSONObject(i + "")
.getJSONObject(i).getString("FIELD20"));
latitudeStations = Double.parseDouble(radioData.getJSONObject(i + "")
.getJSONObject(i).getString("FIELD24"));
Log.i(TAG, "From JSON: " + callSign + frequency + latitudeStations + longitudeStations);
if{
500 >= mRadius;
RadioData radioData = new RadioData();
}
}
return new RadioData();
}