如何找到最近的用户位置标记?
我有10个标记,并且在路线中显示最近的标记。
这是我的MapDirection.class
public class MapDirection extends ActionBarActivity implements GoogleMap.OnMarkerClickListener {
GoogleMap googleMap;
Button btnShowNearestMarker;
Button btnShowDistance;
Button btnShowMarker;
GPSLocationService gpsLocationService;
MarkerVShelter markerVShelter;
private LatLng Mark_Kantor_Gubernur = new LatLng(-0.9376155, 100.3600001);
private LatLng Mark_Bappeda_prov_Sumbar = new LatLng(-0.913884, 100.359176);
private LatLng Mark_Mesjid_Muhajirin = new LatLng(-0.8908614, 100.3538605);
private LatLng Mark_Gedung_Pasar_Raya = new LatLng(-0.9497613, 100.3606601);
private LatLng Mark_SMA_N_1_Pdg = new LatLng(-0.9476911, 100.3625053);
private LatLng Mark_SMK_N_5_Pdg = new LatLng(-0.92146, 100.3518);
private LatLng Mark_SMP_N_7_Pdg = new LatLng(-0.92073, 100.351575);
private LatLng Mark_SMP_N_25_Pdg = new LatLng(-0.920035, 100.3561025);
private LatLng Mark_SD_N_15_Lolong = new LatLng(-0.94086, 100.36316);
private LatLng Mark_SD_N_23_U_Gurun = new LatLng(-0.93373, 100.35444);
private Polyline newPolyline;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_direction_layout);
//Getting Map From Google
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap);
googleMap = supportMapFragment.getMap();
//Set Attribute in first Opening
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.setTrafficEnabled(true);
gpsLocationService = new GPSLocationService(MapDirection.this);
Location gpsLocation = gpsLocationService.getLocation(LocationManager.GPS_PROVIDER);
if (gpsLocation != null) {
double latitude = gpsLocation.getLatitude();
double longitude = gpsLocation.getLongitude();
Toast.makeText(getApplicationContext(), "Mobile Location (GPS) : \nLatitude: " + latitude + "\nLongitude: " + longitude, Toast.LENGTH_LONG).show();
//Handle map in firts opening
LatLng latLng = new LatLng(latitude, longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
// Zoom in, animating the camera.
googleMap.animateCamera(CameraUpdateFactory.zoomIn());
// Zoom out to zoom level 10, animating with a duration of 2 seconds.
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 5000, null);
// Construct a CameraPosition focusing on Mountain View and animate the camera to that position.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng) // Sets the center of the map to Mountain View
.zoom(17) // Sets the zoom
.bearing(180) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
else
{
showGPSAlertDialog();
}
//Setting UP ALL Marker
//Shelter
googleMap.addMarker(new MarkerOptions()
.position(Mark_Kantor_Gubernur)
.title("Kantor Gubernur")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker_icon)));
googleMap.addMarker(new MarkerOptions()
.position(Mark_Bappeda_prov_Sumbar)
.title("Kantor Bappeda Prov. SUMBAR")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker_icon)));
googleMap.addMarker(new MarkerOptions()
.position(Mark_Mesjid_Muhajirin)
.title("Mesjid Muhajirin")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker_icon)));
googleMap.addMarker(new MarkerOptions()
.position(Mark_Gedung_Pasar_Raya)
.title("Gedung Pasar Raya")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker_icon)));
googleMap.addMarker(new MarkerOptions()
.position(Mark_SMA_N_1_Pdg)
.title("Gedung SMA N 1 Padang")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker_icon)));
googleMap.addMarker(new MarkerOptions()
.position(Mark_SMK_N_5_Pdg)
.title("Gedung SMK N 5 Padang")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker_icon)));
googleMap.addMarker(new MarkerOptions()
.position(Mark_SMP_N_7_Pdg)
.title("Gedung SMP N 7 Padang")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker_icon)));
googleMap.addMarker(new MarkerOptions()
.position(Mark_SMP_N_25_Pdg)
.title("Gedung SMP N 25 Padang")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker_icon)));
googleMap.addMarker(new MarkerOptions()
.position(Mark_SD_N_15_Lolong)
.title("Gedung SD N 15 Lolong")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker_icon)));
googleMap.addMarker(new MarkerOptions()
.position(Mark_SD_N_23_U_Gurun)
.title("Gedung SD N 25, Ujung Gurun, Padang")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker_icon)));
//Setting onclick marker & Direction to Marker
googleMap.setOnMarkerClickListener(this);
}
public void showGPSAlertDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MapDirection.this);
//Setting Dialog Title
alertDialog.setTitle("GPS Setting");
//setting dialog message
alertDialog.setMessage("Your GPS is Offline");
//setting button
alertDialog.setPositiveButton("Setting", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
//on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
@Override
public boolean onMarkerClick(Marker marker) {
findDirections(googleMap.getMyLocation().getLatitude(), googleMap.getMyLocation().getLongitude(), marker.getPosition().latitude,
marker.getPosition().longitude, GMapV2Direction.MODE_DRIVING);
return false;
}
public void findDirections(double fromPositionDoubleLat, double fromPositionDoubleLong, double toPositionDoubleLat, double toPositionDoubleLong, String mode) {
Map<String, String> map = new HashMap<String, String>();
map.put(GetDirectionAsyncTask.USER_CURRENT_LAT, String.valueOf(fromPositionDoubleLat));
map.put(GetDirectionAsyncTask.USER_CURRENT_LONG, String.valueOf(fromPositionDoubleLong));
map.put(GetDirectionAsyncTask.DESTINATION_LAT, String.valueOf(toPositionDoubleLat));
map.put(GetDirectionAsyncTask.DESTINATION_LONG, String.valueOf(toPositionDoubleLong));
map.put(GetDirectionAsyncTask.DIRECTIONS_MODE, mode);
GetDirectionAsyncTask asyncTask = new GetDirectionAsyncTask(this);
asyncTask.execute(map);
}
public void handleGetDirectionsResult(ArrayList<LatLng> directionPoints) {
PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.BLUE);
for (int i = 0; i < directionPoints.size(); i++) {
rectLine.add(directionPoints.get(i));
}
if (newPolyline != null) {
newPolyline.remove();
}
newPolyline = googleMap.addPolyline(rectLine);
}
}
查找路线用户位置到标记我使用此Tutorial
答案 0 :(得分:0)
为每个标记绘制从当前位置到标记的路线。你会得到标记的距离。将其存储在数组中。对标记的其余部分执行相同的活动。最后,您将能够确定哪个标记最近,并在地图上绘制其路线。
可以使用以下公式来计算2个坐标之间的距离
int R = 6371; // km
double x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2);
double y = (lat2 - lat1);
double distance = Math.sqrt(x * x + y * y) * R;