【问题标题】:Latitude value set to 0纬度值设置为 0
【发布时间】:2014-12-16 08:34:44
【问题描述】:

我从 MainActivity 给 Fragment 设置了一个值,但是 Fragment 中的值变成了 0。 当我登录 moveToLocation() 时,纬度的值为 0,但是当我登录 setLocation 时,纬度的值是正确的传递值。 这是为什么?

主活动

MapFragment mapFragment = new MapFragment();
mapFragment.setLocation(111, 222, "abcAddress");

片段

public class MapFragment extends Fragment {
    private GoogleMap mMap;
    private double mLatitude;
    private double mLongitude;
    private String mAddress;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            Log.d("", "You must update Google Maps.");
            getActivity().finish();
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mMap = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
        moveToLocation();
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.map_page, null);
    }

    public void setLocation(double latitude, double longitude, String address) {
        mLatitude = latitude;
        mLongitude = longitude;
        mAddress = address;
    }

    protected void moveToLocation() {
        CameraPosition location = new CameraPosition.Builder()
                .target(new LatLng(mLatitude, mLongitude)).zoom(15.5f)
                .bearing(0).tilt(25).build();
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(location));
        MarkerOptions marker = new MarkerOptions().position(new LatLng(mLatitude, mLongitude)).title(mAddress);
        marker.snippet("Snippet");
        mMap.addMarker(marker);
    }
}

【问题讨论】:

  • 显示更多使用替换、提交片段的活动代码。
  • 您在尝试设置位置时是否正在创建 MapFragment 的新实例?
  • 我不在 Activity 中使用替换和提交。 Activity 中的其他代码与 Fragment 没有关系。
  • 是的,我创建了 MapFragment 的新实例
  • 在显示此片段时使用myFragment.setArguments(bundle); 传递值。也显示该代码

标签: android google-maps


【解决方案1】:

您不应创建MapFragment 的新实例。

要传递值,请在 Argument 中设置它们,同时在 Transaction 中添加它们:

Bundle bundle = new Bundle();
bundle.putDouble("lat", latitude);
bundle.putDouble("long", longitude);
bundle.putString("address", address);
myFragment.setArguments(bundle);

并获取onCreate() 中的值,例如:

@Override
public View onCreate(Bundle savedInstanceState) {

    Bundle mBundle = getArguments();

    if (mBundle != null) {
        String address = mBundle.getString("address");
        // Call method
        setLocation(mBundle.getDouble("lat"), mBundle.getDouble("long"), mBundle.getString("address"));
    } else {
        Toast.makeText(getActivity(), "Location Not Found", Toast.LENGTH_SHORT).show();
    }

}

希望对你有所帮助ツ

【讨论】:

  • 当你创建一个 MapFragment 的实例以首先显示 MAP
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-10
  • 1970-01-01
  • 1970-01-01
  • 2013-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多