如何将onMarkerClick中的markerPos变量传递给private void方法?
如何将onMarkerClick中的markerPos值传递到getCurrentlocation方法中作为目标点
我需要把onMarkerClick方法里获取到的markerPos(LatLng类型)变量值,传递到private void类型的getCurrentlocation方法中,将它设置为位置的目标点。请问怎么在getCurrentlocation方法内获取这个值?
相关代码
@Override public boolean onMarkerClick(@NonNull Marker marker) { int id = markers.get(marker.getId()); // 需要传递的变量 LatLng markerPos = marker.getPosition(); String id1 = marker.getTitle(); // 每次点击Marker时清除折线 for(Polyline line : polylines) { line.remove(); } polylines.clear(); Toast.makeText(this, "You clicked " + id1, Toast.LENGTH_LONG).show(); if (id == 2) { // 添加折线到地图 polylines.add(this.mMap.addPolyline(new PolylineOptions() .clickable(true) .add( new LatLng(13.7926088,121.0025078), new LatLng(13.7924463,121.0025413), new LatLng(13.792438, 121.002805), new LatLng(13.792281, 121.002861), new LatLng(13.792288, 121.003093)))); getCurrentlocation(); } return false; } private void getCurrentlocation() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { fusedLocationProviderClient.getLastLocation() .addOnSuccessListener(new OnSuccessListener<Location>() { @Override public void onSuccess(Location location) { if(location != null){ Geocoder geocoder = new Geocoder(MapsActivity.this, Locale.getDefault()); List<Address> addresses = null; try { addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); LatLng userloc = new LatLng((addresses.get(0).getLatitude()), (addresses.get(0).getLongitude())); MarkerOptions userlocM = new MarkerOptions() .position(userloc) .title("You") .icon(BitmapDescriptorFactory.fromBitmap(resizeBitmap("userloc",160,160))); Marker mkr3 = mMap.addMarker(userlocM); markers.put(mkr3.getId(), 3); mOrigin = userloc; // 这里需要设置markerPos值给mDestination mDestination =; drawRoute(); } catch (IOException e) { throw new RuntimeException(e); } } } }); } else { askPermission(); } }
可行实现方式
方法一:修改getCurrentlocation方法参数,直接传入markerPos
这是最清晰直接的方式,让方法接收需要的目标点参数:
- 修改
getCurrentlocation的方法签名,添加LatLng destination参数:
private void getCurrentlocation(LatLng destination) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { fusedLocationProviderClient.getLastLocation() .addOnSuccessListener(new OnSuccessListener<Location>() { @Override public void onSuccess(Location location) { if(location != null){ // 原有代码保持不变 // ... // 直接使用传入的destination赋值给mDestination mDestination = destination; drawRoute(); } } }); } else { askPermission(); } }
- 在
onMarkerClick方法中调用getCurrentlocation时传入markerPos:
if (id == 2) { // 原有添加折线的代码不变 // ... getCurrentlocation(markerPos); // 传入markerPos }
方法二:使用类成员变量存储markerPos
如果不想修改方法参数,可以在Activity类中定义一个成员变量来临时保存选中的Marker位置:
- 在你的
MapsActivity类中添加成员变量:
// 类内部定义,在方法外 private LatLng mSelectedMarkerPosition;
- 在
onMarkerClick方法中给这个成员变量赋值:
LatLng markerPos = marker.getPosition(); mSelectedMarkerPosition = markerPos; // 保存到成员变量
- 在
getCurrentlocation方法中直接使用该成员变量:
// 原有代码不变 // ... mDestination = mSelectedMarkerPosition; drawRoute();
推荐使用方法一,因为它符合单一职责原则,让getCurrentlocation方法的依赖更明确,代码可读性更高,也避免了成员变量可能带来的状态混乱问题。
内容的提问来源于stack exchange,提问作者user21436208
相关产品推荐
相关产品推荐

