gpt4 book ai didi

android - 更新之前的经纬度

转载 作者:行者123 更新时间:2023-11-29 20:45:51 27 4
gpt4 key购买 nike

非常感谢您的帮助、知识和时间,这对我来说是无价的。我正在开发 GPS 定位应用程序。基本思想是指南针的行为。如果朝纬度方向行走,那么我将收到关于我的方向是向北还是向南的通知,与经度相同。

因此,我必须保存已知的过去位置和当前位置,以便在它们之间进行比较并显示特定通知。到现在为止,我能够恢复当前位置和第一个位置,但我无法恢复前一个位置以与当前位置进行比较。我将不得不比较这两点之间的纬度和经度。因此,之前的位置和实际位置必须根据移动而变化,但我只能获得实际位置的更新。

我分享我拥有的源代码:

主要 Activity .java:

package com.example.locationservicetest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
TextView latitude;
TextView longitude;
TextView prevLatitude;
TextView prevLongitude;
TextView provText;
double lat1;
double lon1;
LocationManager locationManager;
String provider;
MyLocationListener mylistener;
Criteria criteria;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitude = (TextView) findViewById(R.id.lat);
longitude = (TextView) findViewById(R.id.lon);
prevLatitude = (TextView) findViewById(R.id.prevLat);
prevLongitude = (TextView) findViewById(R.id.prevLon);
provText = (TextView) findViewById(R.id.prov);

//Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

//Define the criteria how to select the location provider
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); //default

//Charge of data?
criteria.setCostAllowed(false);

//Get the best provider depending on the criteria
provider = locationManager.getBestProvider(criteria, false);

//To receive the first location use cached location
Location location = locationManager.getLastKnownLocation(provider);
Location mLastLocation;

mylistener = new MyLocationListener();

if (location != null) {
mylistener.onLocationChanged(location);
mLastLocation=location;
lat1=mLastLocation.getLatitude();
lon1=mLastLocation.getLongitude();
prevLatitude.setText("Previous Latitude: "+String.valueOf(lat1));
prevLongitude.setText("Previous Longitude: "+String.valueOf(lon1));

} else {
//leads to the settings because there is no last known location
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
// location updates: at least 1 meter and 200millsecs change
locationManager.requestLocationUpdates(provider, 200, 1, mylistener);
}

private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
// Initialize the location fields
latitude.setText("Latitude: "+String.valueOf(location.getLatitude()));
longitude.setText("Longitude: "+String.valueOf(location.getLongitude()));

Toast.makeText(getBaseContext(),"Location changed!",
Toast.LENGTH_SHORT).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(getBaseContext(), provider + "'s status changed to "+status +"!",
Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getBaseContext(), "Provider " + provider + " enabled!",
Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getBaseContext(), "Provider " + provider + " disabled!",
Toast.LENGTH_SHORT).show();
}
}
}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/prov"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No provider selected yet"
android:layout_marginTop="10dp"
android:textSize="20dp" />
<TextView
android:id="@+id/lat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Latitude: -"
android:textSize="20dp" />
<TextView
android:id="@+id/lon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Longitude: -"
android:textSize="20dp" />
<TextView
android:id="@+id/prevLat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="PrevLatitude: -"
android:textSize="20dp" />
<TextView
android:id="@+id/prevLon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="PrevLongitude: -"
android:textSize="20dp" />
</LinearLayout>

提前感谢您的好意

最佳答案

我不确定你到底想要什么无论如何,这里有一些可能对您有用的建议

为了获得准确的数据,最好只使用 GPS 提供商,200 毫秒可能很短,您可能希望将其设为 1+ 秒

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);

也不需要调用 getLastKnownLocation 就可以开始监听和使用这些更新的值这是“MyLocationListener”类的简化实现

public class MyLocationListener implements LocationListener{
private Location previousLocation;
@Override
public void onLocationChanged(Location location) {
if(previousLocation != null ){
//i already cached a previousLocation
//most recent (lat , lng) are stored in location
//you can access the previous (lat.lng) from previousLocation
//TODO:do your calculation and Send your notification here
}else{
//this is the first location i got i dont have anything to comapare it with
//nothing to do here i just added it for clarity
}
previousLocation = location;
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
//TODO:implement if you need it
}

@Override
public void onProviderEnabled(String s) {
//TODO:implement if you need it
}

@Override
public void onProviderDisabled(String s) {
//TODO:implement if you need it
}
}

关于android - 更新之前的经纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30386444/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com