[英]Calculating the distance between two markers in Android
對於我當前正在使用的應用程序,我想設置一個按鈕來查找google maps活動上兩個標記之間的距離,當您單擊該按鈕時,它會顯示您當前位置與另一個我不知道的標記之間的距離我的java類中沒有用於按鈕的任何代碼,但是此刻,我只是想知道如何實際找到當前位置和設置標記之間的距離。 這是我用於查找用戶當前位置的代碼,它在隨機位置中設置了標記。
package dashpage.example.com.myapplication;
import android.content.IntentSender;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
public static final String TAG = MapsActivity.class.getSimpleName();
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.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
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
mGoogleApiClient.connect();
}
@Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(53.3835, 6.5996)).title("Marker"));
}
private void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
//mMap.addMarker(new MarkerOptions().position(new LatLng(currentLatitude, currentLongitude)).title("Current Location"));
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am here!");
mMap.addMarker(options);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
@Override
public void onConnected(Bundle bundle) {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
else {
handleNewLocation(location);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
/*
* Thrown if Google Play services canceled the original
* PendingIntent
*/
} catch (IntentSender.SendIntentException e) {
// Log the error
e.printStackTrace();
}
} else {
/*
* If no resolution is available, display a dialog to the
* user with the error.
*/
Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
@Override
public void onLocationChanged(Location location) {
handleNewLocation(location);
}
}
我也知道,要在Android Studio中查找兩個標記之間的距離,您可以使用類似
Location loc1 = new Location("");
loc1.setLatitude(lat1);
loc1.setLongitude(lon1);
Location loc2 = new Location("");
loc2.setLatitude(lat2);
loc2.setLongitude(lon2);
float distanceInMeters = loc1.distanceTo(loc2);
因此,我只是想知道是否有人能夠幫助我實現用於查找距離的代碼,因為我不確定該距離在我的課堂中應該走到哪里,還是我是否必須重做該課程的某些部分才能制作出距離?遠程工作
嘗試查找標記之間的距離時,建議將它們轉換回Location
對象,以便能夠使用內置的Android計算距離的方法。
給定您已設置的一些Marker marker
,並可以通過onMarkerClick()
或已經保存的Marker marker
以及當前位置Location currentLocation
,在Button
onClick(View v)
方法中:
LatLng markerLatLng = marker.getPosition();
Location markerLocation = new Location("");
markerLocation.setLatitude(markerLatLng.latitude);
markerLocation.setLongitude(markerLatLng.longitude);
currentLocation.distanceTo(markerLocation);
嗨,我舉了一個例子,就像您的qquestion一樣,但是您必須為自己的構建進行自定義。您可以使用Collections類,它簡單而完美地工作。
Comparator<Sinemalar> comparator=new Comparator<Sinemalar>() {
@Override
public int compare(Sinemalar left, Sinemalar right) {
return (int)(left.getDistance()-right.getDistance());
}
};
Collections.sort(sinemalarList, comparator);
我希望為你工作
public float distanceCounter(String value, Context context) {
Location cinemaLocation = new Location("CinemaLocation");
String s = new String(value);
String[] result = s.split(",");
List<String> elephantList = Arrays.asList(s.split(","));
String longitude = elephantList.get(1);
String latitude = elephantList.get(0);
cinemaLocation.setLatitude(Double.parseDouble(latitude));
cinemaLocation.setLongitude(Double.parseDouble(longitude));
float distance = getCurrrentLocation(context).distanceTo(cinemaLocation)/1000 ;
return distance;
}
字符串值= 34.54665、23.54546
您可以在此處使用完整的代碼。很抱歉,我忘記了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.