如何从Android上的回调方法访问变量

时间:2016-12-20 07:18:19

标签: android callback android-mapview

我有一个名为MapViewManager的类,其中有一个名为navigate的方法,如下所示:

public class MapViewManager{
    ...
    public void navigate(double startX, double startY, double toX, double toY, long floorId, boolean flag) {
        final FeatureLayer featureLayer = new FeatureLayer("navigate");
        mapView.setLayerOffset(featureLayer);
        mapView.addLayer(featureLayer);

        final NavigateManager navigateManager = new NavigateManager();
        if (flag) {
            navigateManager.navigation(startX, startY, floorId, toX, toY, floorId);
        }else{
            navigateManager.clear();
        }

        navigateManager.setOnNavigateComplete(new NavigateManager.OnNavigateComplete() {
            @Override
            public void onNavigateComplete(NavigateManager.NavigateState navigateState,
                                       FeatureCollection featureCollection) {
                featureLayer.clearFeatures();
                featureLayer.addFeatures(featureCollection);

                for (int i=0;i<featureCollection.getSize();i++){
                    Feature feature = featureCollection.getFeature(i);
                    Coordinate coordinate = feature.getCentroid();
                    double x = coordinate.getX();
                    double y = coordinate.getY();   
                }
            }
        });
    }
}

我想从外部访问onNavigateComplete回调方法中名为coordinate的变量,以便其他类可以使用坐标变量来执行某些操作。我该怎么做?

2 个答案:

答案 0 :(得分:0)

你必须通过这样做公开:

说出您的活动名称是&#39; Main&#39;

cmake_minimum_required(VERSION 3.5.2)
project(ios_minicap)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -g")

set(SOURCE_FILES
        src/minicap.cpp
        src/SimpleServer.cpp src/SimpleServer.hpp
        src/FrameListener.cpp src/FrameListener.hpp
        src/Banner.cpp src/Banner.hpp
        src/JpegEncoder.cpp src/JpegEncoder.hpp
        src/StreamClient.mm src/StreamClient.h
        src/Frame.hpp)

add_executable(ios_minicap ${SOURCE_FILES})

include_directories(
        /usr/local/opt/jpeg-turbo/include
)

target_link_libraries (ios_minicap
        "-framework Foundation"
        "-framework CoreFoundation"
        "-framework CoreMedia"
        "-framework CoreVideo"
        "-framework CoreMediaIO"
        "-framework AVFoundation"
        /usr/local/opt/jpeg-turbo/lib/libturbojpeg.a)

答案 1 :(得分:0)

在导航方法中添加一个参数。

public class MapViewManager{
    ...
    public void navigate(double startX, double startY, double toX, double toY, long floorId, boolean flag, NavigateManager.OnNavigateComplete navigation) {
        final FeatureLayer featureLayer = new FeatureLayer("navigate");
        mapView.setLayerOffset(featureLayer);
        mapView.addLayer(featureLayer);

        final NavigateManager navigateManager = new NavigateManager();
        if (flag) {
            navigateManager.navigation(startX, startY, floorId, toX, toY, floorId);
        }else{
            navigateManager.clear();
        }

        navigateManager.setOnNavigateComplete(navigation);
    }
}

从其他类调用方法:

new MapViewManager().navigate(other paramter values, new NavigateManager.OnNavigateComplete() {
            @Override
            public void onNavigateComplete(NavigateManager.NavigateState navigateState,
                                       FeatureCollection featureCollection) {
                featureLayer.clearFeatures();
                featureLayer.addFeatures(featureCollection);

                for (int i=0;i<featureCollection.getSize();i++){
                    Feature feature = featureCollection.getFeature(i);
                    Coordinate coordinate = feature.getCentroid();
                    navigationPoints.add(coordinate);
                    double x = coordinate.getX();
                    double y = coordinate.getY();   
                }
            }
        });

现在,您可以随处访问任何对象。