在运行时通过customView锚定SKAnnotation

时间:2015-12-15 17:48:30

标签: android skmaps

运行SDK 2.5.1,我尝试使用我的customView添加自己的SKAnnotations,其大小根据显示的名称动态变化。为此,我附加了OnGlobalLayoutListener,以相应地设置注释的偏移量。然而,似乎全球听众根本没有被召唤。我已经尝试将它连接到mapView.getAnnotationView()。getView(),mapView和片段的mapHolder,没有结果。任何允许我将我的注释锚定到实际位置的解决方案将不胜感激。

final SKAnnotation newDelhi = new SKAnnotation(444);
    newDelhi.setMininumZoomLevel(5);
    newDelhi.setAnnotationType(SKAnnotation.SK_ANNOTATION_TYPE_MARKER);
    SKAnnotationView newDelhiAnnotationView = new SKAnnotationView();
    RelativeLayout newDelhiCustomView =
            (RelativeLayout) ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.annotation_layout, null, false);
    newDelhiCustomView.setTag("new delhi");
    newDelhiAnnotationView.setView(newDelhiCustomView);
    newDelhi.setAnnotationView(newDelhiAnnotationView);
    TextView newDelhiTextView = (TextView) newDelhiAnnotationView.getView().findViewById(R.id.annotation_textview);
    newDelhiTextView.setText("New Delhi");
    SKCoordinate newDelhiLocation = new SKCoordinate(77.179163, 28.619828);
    newDelhi.setLocation(newDelhiLocation);
    addRedPin(newDelhiLocation, 445);
    mapView.addAnnotation(newDelhi, SKAnimationSettings.ANIMATION_NONE);
    ViewTreeObserver vto = mapHolder.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            mapHolder.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            float width = newDelhi.getAnnotationView().getView().getWidth();
            float height = newDelhi.getAnnotationView().getView().getHeight();
            newDelhi.setOffset(new SKScreenPoint(-width / 2, height / 2));
            mapView.updateAnnotation(newDelhi);
        }
    });

    final SKAnnotation chandigarh = new SKAnnotation(555);
    chandigarh.setMininumZoomLevel(5);
    chandigarh.setAnnotationType(SKAnnotation.SK_ANNOTATION_TYPE_MARKER);
    SKAnnotationView chandigahrAnnotationView= new SKAnnotationView();
    RelativeLayout chandigarhCustomView =
            new RelativeLayout(getActivity());
    chandigarhCustomView.setTag("chandigarh");
    TextView chandigarhTextView = new TextView(getActivity());
    chandigarhTextView.setText("Chandigarh");
    chandigarhCustomView.addView(chandigarhTextView);
    chandigahrAnnotationView.setView(chandigarhCustomView);
    chandigarh.setAnnotationView(chandigahrAnnotationView);
    SKCoordinate chandigarhLocation = new SKCoordinate(76.778867,30.736094);
    chandigarh.setLocation(chandigarhLocation);
    mapView.addAnnotation(chandigarh, SKAnimationSettings.ANIMATION_NONE);
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {                mapHolder.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            float width = chandigarh.getAnnotationView().getView().getWidth();
            float height = chandigarh.getAnnotationView().getView().getHeight();
            chandigarh.setOffset(new SKScreenPoint(-width / 2, height / 2));
            mapView.updateAnnotation(newDelhi);
        }
    });

    addRedPin(chandigarhLocation, 556);

以下是注释错误显示的截图(实际位置为红色引脚): enter image description here

0 个答案:

没有答案