我在地图中创建了当前位置。还有一个Spinner视图来选择目标位置。在我的情况下,我想从当前位置和选定位置获取纬度和经度。我想在onInfoWindowClick()
方法中执行这些值。
有人能帮我吗?
这是我的代码
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (mLocationPermissionGranted){
getDeviceLocation();
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
}
SQLiteDatabase db = dbHelper.getReadableDatabase();
cursor = db.rawQuery("SELECT * FROM atm", null);
if (cursor != null){
while (cursor.moveToNext()){
title = cursor.getString(1).toString();
__global_endposition = cursor.getString(2).toString();
String[] exp_endCoordinate = __global_endposition.split(",");
final double lat_endposition = Double.parseDouble(exp_endCoordinate[0]);
double lng_endposition = Double.parseDouble(exp_endCoordinate[1]);
LatLng endx = new LatLng(lat_endposition, lng_endposition);
MarkerOptions options = new MarkerOptions();
options.position(endx);
options.title(title);
options.snippet(__global_endposition);
if (title.equals("ATM BNI")){
options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
}else if(title.equals("ATM BCA")){
options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
}else if(title.equals("ATM Mandiri")){
options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
}
Marker marker = mMap.addMarker(options);
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(getActivity()));
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
// Take LatLong current Location and LatLong selected location
}
});
}
if (!cursor.isClosed()) {
cursor.close();
cursor = null;
}
}
mMap.getUiSettings().setZoomControlsEnabled(true);
}
这是我的getDeviceLocation()
代码:
private void getDeviceLocation(){
Log.d(TAG, "geDeviceLocation: mendapatkan lokasi pengguna");
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
try {
if (mLocationPermissionGranted){
Task location = mFusedLocationProviderClient.getLastLocation();
location.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()){
Log.d(TAG, "onComplete: lokasi ditemukan");
Location currentLocation = (Location) task.getResult();
// public variable
mLatitude = currentLocation.getLatitude();
mLongitude = currentLocation.getLongitude();
if (currentLocation != null){
moveCamera(new LatLng(mLatitude, mLongitude),
DEFAULT_ZOOM);
}
}else{
Log.d(TAG, "onComplete: lokasi tidak ditemukan");
Toast.makeText(getActivity(), "Lokasi tidak ditemukan", Toast.LENGTH_SHORT).show();
}
}
});
}
}catch (SecurityException e){
Log.e(TAG, "geDeviceLocation: SecurityException"+e.getMessage() );
}
}