- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
解释: 我知道这个问题大部分都是重复的,但我有点困惑。我正在处理实时应用程序。其中,我在 json 中读取了 JSON 数据,那里有一些经度和纬度。我获取与当前经度和纬度的比较。我找到了一些方法并尝试了但没有得到预期的输出。
我想比较我当前的经纬度在距离json经纬度五公里(km)以下的时候。如果用户超出范围,例如超过五公里,则会显示消息用户超出范围。
这是我获取经度和纬度的代码
public class HomeFragment extends Fragment implements ConnectionCallbacks, OnConnectionFailedListener,LocationListener{
public static final String TAG=TransactionFragment.class.getSimpleName();
public static final String KEY_UGC="postData[ugc]";
public static final String KEY_CUSTOMER_ID="postData[customerid]";
public static final String KEY_UGC_ID="postData[ugcid]";
View rootView;
PreferenceSetting prefManager;
ListView lvTransaction;
List<String> items;
TextView txtPoints;
Button btnUgc,btnMore;
List<Trasaction> trasactionList;
TextView lblTrasaction,lblStation,lblNoBottle,lblPointsColl,lblPoints;
int totalPoints=0;
MainActivity activity;
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private double currentLatitude;
private double currentLongitude;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView=inflater.inflate(R.layout.fragment_home,container,false);
prefManager=PreferenceSetting.getInstance(getActivity());
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
// The next two lines tell the new client that “this” current class will handle connection stuff
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
//fourth line adds the LocationServices API endpoint from GooglePlayServices
.addApi(LocationServices.API)
.build();
// Create the LocationRequest object
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds
activity=(MainActivity)getActivity();
lblTrasaction=(TextView)rootView.findViewById(R.id.lbl_trasaction);
lblTrasaction.setTypeface(AppController.fntAlice);
lblStation=(TextView)rootView.findViewById(R.id.lbl_station);
lblStation.setTypeface(AppController.fntVani);
lblNoBottle=(TextView)rootView.findViewById(R.id.lbl_no_bottle);
lblNoBottle.setTypeface(AppController.fntVani);
lblPointsColl=(TextView)rootView.findViewById(R.id.lbl_points_coll);
lblPointsColl.setTypeface(AppController.fntVani);
lblPoints=(TextView)rootView.findViewById(R.id.lbl_points);
lblPoints.setTypeface(AppController.fntVani);
btnUgc=(Button)rootView.findViewById(R.id.btn_ugc);
btnUgc.setTypeface(AppController.fntVani);
btnMore=(Button)rootView.findViewById(R.id.btn_more);
btnMore.setTypeface(AppController.fntVani);
txtPoints=(TextView)rootView.findViewById(R.id.txt_points);
txtPoints.setTypeface(AppController.fntVani);
if(prefManager.getPoints()==0){
txtPoints.setText("0");
}
else{
txtPoints.setText(""+prefManager.getPoints());
}
if(Utils.isNetworkConnected(getActivity())){
RequestParams params=new RequestParams();
params.put(ApiCalls.REQUEST_CASE_LABEL,"customerHistory");
params.put("postData[customerid]",prefManager.getCustomerId());
getTransactionApiCall(params);
}
else{
Toast.makeText(getActivity(),getString(R.string.internet), Toast.LENGTH_SHORT).show();
}
items=new ArrayList<>();
lvTransaction=(ListView)rootView.findViewById(R.id.lv_transaction);
btnMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Message message=new Message();
Bundle bundle=new Bundle();
bundle.putString("more","1");
message.setData(bundle);
activity.uiHandler.sendMessage(message);
}
});
btnUgc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialog();
}
});
return rootView;
}
@Override
public void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
@Override
public void onPause() {
super.onPause();
Log.v(this.getClass().getSimpleName(), "onPause()");
//Disconnect from API onPause()
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnected(Bundle bundle) {
if (Build.VERSION.SDK_INT >= 21 &&
ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return ;
}
else {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
}
}
}
@Override
public void onConnectionSuspended(int i) {}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try {
connectionResult.startResolutionForResult(getActivity(), CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
} else {
Log.e("Error", "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
@Override
public void onLocationChanged(Location location) {
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
}
public void getUcgCallApi(final RequestParams params){
AsyncHttpClient client=new AsyncHttpClient();
client.setTimeout(AsyncHttpClient.DEFAULT_SOCKET_TIMEOUT);
client.post(ApiCalls.API_CALL, params, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
super.onStart();
Log.i(TAG,"GET UGC PARAMS:->>>"+params.toString());
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try{
String str=new String(responseBody);
JSONObject mainObj=new JSONObject(str);
Log.e(TAG,"SUCCESS RESPONSE:=>>"+mainObj.toString());
if(mainObj.has("status") && !mainObj.isNull("status")){
if(mainObj.getString("status").equalsIgnoreCase("Success")){
JSONArray data=mainObj.getJSONArray("data");
JSONObject dataObj=data.getJSONObject(0);
prefManager.setPkUgcId(dataObj.getString("PK_UGC_ID"));
prefManager.setUgcCode(dataObj.getString("UGC_CODE"));
if(!prefManager.getPkUgcId().equals(PreferenceSetting.DEFAULT)){
Log.e(TAG,"UGC ID STORED::"+prefManager.getPkUgcId()+"\nUGC CODE::->"+prefManager.getUgcCode());
Date timeDate=Utils.getDate(Long.parseLong(dataObj.getString("EXPIRED_TIME")));
Log.e(TAG,"DATE UTIL:->"+timeDate);
Date currentDate=new Date();
double LOCATION_LATITUDE=Double.parseDouble(dataObj.getString("LOCATION_LATITUDE"));
double LOCATION_LONGITUDE=Double.parseDouble(dataObj.getString("LOCATION_LONGITUDE"));
Log.e(TAG,"GETCURRENT TIME:->"+currentDate.toString());
if(timeDate.after(currentDate)){
Log.e(TAG,"FIRST DISTANCE:->"+distance(currentLatitude,currentLongitude,LOCATION_LATITUDE,LOCATION_LONGITUDE));
if((distance(currentLatitude,currentLongitude,LOCATION_LATITUDE,LOCATION_LONGITUDE)/1000) <= 5 ) {
Log.e(TAG,"AFTER DISTANCE:->"+distance(currentLatitude,currentLongitude,LOCATION_LATITUDE,LOCATION_LONGITUDE));
RequestParams reqParams = new RequestParams();
reqParams.put(ApiCalls.REQUEST_CASE_LABEL, "UGCReading");
reqParams.put(KEY_UGC, prefManager.getUgcCode());
reqParams.put(KEY_CUSTOMER_ID, prefManager.getCustomerId());
reqParams.put(KEY_UGC_ID, prefManager.getPkUgcId());
// readUgcCodeApiCall(reqParams);
}
else{
Toast.makeText(getActivity(), "“You are out of range for redeem", Toast.LENGTH_SHORT).show();
}
}
if(timeDate.before(currentDate)){
Toast.makeText(getActivity(), "UGC Code Expired", Toast.LENGTH_SHORT).show();
}
}
}
else{
Toast.makeText(getContext(), mainObj.getString("status"), Toast.LENGTH_SHORT).show();
}
}
}
catch (Exception e){
Log.e(TAG,"on Success()==>"+Log.getStackTraceString(e));
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
try{
String str=new String(responseBody);
JSONObject mainObj=new JSONObject(str);
Log.e(TAG,"FAILURE RESPONSE:=>>"+mainObj.toString());
}
catch (Exception e){
Log.e(TAG,"on Failure()==>"+Log.getStackTraceString(e));
}
}
});
}
public void readUgcCodeApiCall(final RequestParams params){
AsyncHttpClient client=new AsyncHttpClient();
client.setTimeout(AsyncHttpClient.DEFAULT_SOCKET_TIMEOUT);
client.post(ApiCalls.API_CALL, params, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
super.onStart();
Log.e(TAG,"PARAMS:->>>"+params.toString());
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
String str=new String(responseBody);
JSONObject mainObj=new JSONObject(str);
Log.e(TAG,"SUCCESS RESPONSE readUgcCodeApiCall :=>>"+mainObj.toString());
if(mainObj.has("status") && !mainObj.isNull("status")){
if(mainObj.getString("status").equalsIgnoreCase("Success")){
Toast.makeText(getActivity(),getString(R.string.success), Toast.LENGTH_SHORT).show();
RequestParams params=new RequestParams();
params.put(ApiCalls.REQUEST_CASE_LABEL,"customerHistory");
params.put("postData[customerid]",prefManager.getCustomerId());
getTransactionApiCall(params);
}
else{
Toast.makeText(getActivity(),getString(R.string.unsuccess), Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
Log.e(TAG,"on Success()==>"+Log.getStackTraceString(e));
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
try {
String str=new String(responseBody);
JSONObject mainObj=new JSONObject(str);
Log.e(TAG,"FAILURE RESPONSE:=>>"+mainObj.toString());
} catch (Exception e) {
Log.e(TAG,"on Failure()==>"+Log.getStackTraceString(e));
}
}
});
}
private double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return (dist);
}
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
public void showDialog(){
final Dialog dialog=new Dialog(getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.claim_dialog);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCanceledOnTouchOutside(true);
dialog.setCancelable(true);
final EditText editClaim=(EditText)dialog.findViewById(R.id.edit_claim);
// editClaim.setTypeface(AppController.fntVani);
Button btnClaim=(Button)dialog.findViewById(R.id.btn_claim);
btnClaim.setTypeface(AppController.fntVani);
btnClaim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!editClaim.getText().toString().trim().equals("")) {
if (Utils.isNetworkConnected(getActivity())) {
RequestParams params=new RequestParams();
params.put(ApiCalls.REQUEST_CASE_LABEL,"getUGCDetails");
params.put(KEY_UGC,editClaim.getText().toString().trim());
getUcgCallApi(params);
if(dialog.isShowing()){
dialog.dismiss();
}
} else {
Toast.makeText(getContext(), getString(R.string.internet), Toast.LENGTH_SHORT).show();
}
}
}
});
dialog.show();
}
}
JSON 响应
在上面的代码中有一个方法,即 getUcgCallApi(),在此我尝试比较旧的和当前的经度和纬度。
请帮我解决这个问题。
提前致谢。
最佳答案
试试这个来获取两个经纬度之间的距离:
float[] distance = new float[1];
Location.distanceBetween(mLocation.latitude, mLocation.longitude, locations.latitude, locations.longitude, distance);
计算出的距离存储在“distance[0]”中。如果 distance 的长度为 2 或更大,则初始方位角存储在“distance[1]”中。如果 distance 的长度为 3 或更大,则最终方位角存储在“distance[2]”中。
关于android - 我怎样才能在android中以公里为单位获得两个经度和两个纬度之间的距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39204469/
我正在为我的雇主编写脚本,以从他们自己的站点获取某些数据。出于一长串原因,我需要从网站上获取数据,如图所示。我发现,其中一些数据是通过 js 调用检索的... 回想起来,我应该选择 Mechanize
我正在使用 python 和 cryptography.io 来签署和验证消息。我可以通过以下方式获得签名的 DER 编码字节表示: cryptography_priv_key.sign(messag
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
是否可以区分 ECDF?以下面得到的为例。 set.seed(1) a <- sort(rnorm(100)) b <- ecdf(a) plot(b) 我想对 b 求导以获得它的概率密度函数 (PD
我找到了如何从 navigator.mimeTypes 获取 mimetypes: function GetMimeTypes() { var message = ""; var mi
我在表单中使用单选按钮来隐藏/显示联系人表单中的成员 ID 字段。问题是,当 javascript 更改 html 中包含的隐藏 id 字段(该字段设置为“无”值)时,该字段将不再通过 post 可用
我正在做单元测试。我必须测试所有可能的if..else情况。但是在此if语句中: int32_t i32Res = snprintf(buffer, len, "The%d_String_%d", 0
我有一个 Facebook 应用程序,我想从中获取“喜欢”的总数。我想知道这是否可能。 其中 ID 是应用程序的 ID,ACCESS_TOKEN 是我尝试过的应用程序的当前访问 token : gra
如果我有多个计算实例尝试同时获取同一个 blob 的租约,则似乎经常会成功。我的印象是,一旦租约发出(并因此被客户获得),就不可能同时发出另一个租约? 我希望情况确实如此,我一直在 Azure 中使用
这是我的索引 POST /blogs/1 { "name" : "learn java", "popularity" : 100 } POST /blogs/2 { "name" : "l
我正在将 Symfony2 与 FOSUserBundle 一起使用。我需要为用户获得最高角色。 role_hierarchy: ROLE_CONTRIBUTOR: ROLE_USER
我正在向服务器发送基于 REST 的请求。我希望尽快得到答复,并希望了解可以进行的各种优化。 一种方法当然是在线程中并行发送这些请求。还有哪些其他选项可用于优化此功能? 在服务器上,可以添加哪些配置?
这可能是某种重复的问题,但我似乎找不到合适的解决方案。我正在使用 git4idea.history.GitHistoryUtils.history() 获取提交列表。如果 checkout 其中一个较
我正在做一个程序,可以输入每周的工资和那一周的总工作时间。它应该以小时工资率显示答案。但是我无法显示正确的“centavos/2 decimal places”公式并且它不想使用 float % fl
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我已经尝试了 mContext.getMainLooper() 和 Looper.getMainLooper()。两者都返回相同的结果,但我想知道哪种方法正确? 我还从 Android 开发人员链接中
我有一个“affiliates”表,其中包含“user”和“referredBy”列。 给定一个用户,我希望获得该用户推荐的所有“n 级”玩家。对于 n=1,我们只关心您直接推荐的玩家数量: SELE
我在 PostgreSQL 9.5 数据库中有两个表: project - id - name task - id - project_id - name - updated_
请帮助我怎样才能得到我预期的结果,在此先感谢并抱歉我的英语不好。 PHP: $dog = implode(',', $data['dogbreed']); $query .= "AND `do
我有 let impulse = CGVectorMake(CGFloat(Constants.impulse), 0) 如何在不创建另一个 CGVector 的情况下得到它的负值? 我正在考虑在 C
我是一名优秀的程序员,十分优秀!