有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用getMap()获取错误?

我正在尝试交互式infoWindow,其中一个答案是堆栈溢出

链接如下:

interactive infowindow

但代码中使用的getMap()出现了错误。虽然我尝试使用getMapAsync,但无法解决问题。请尽快帮我。如果有任何新代码可用于带有按钮的交互式信息窗口,请共享该代码

    public class MainActivity extends Activity {    
    private ViewGroup infoWindow;
    private TextView infoTitle;
    private TextView infoSnippet;
    private Button infoButton;
    private OnInfoWindowElemTouchListener infoButtonListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
        final MapWrapperLayout mapWrapperLayout = (MapWrapperLayout)findViewById(R.id.map_relative_layout);
        final GoogleMap map = mapFragment.getMap();

        // MapWrapperLayout initialization
        // 39 - default marker height
        // 20 - offset between the default InfoWindow bottom edge and it's content bottom edge 
        mapWrapperLayout.init(map, getPixelsFromDp(this, 39 + 20)); 

        // We want to reuse the info window for all the markers, 
        // so let's create only one class member instance
        this.infoWindow = (ViewGroup)getLayoutInflater().inflate(R.layout.info_window, null);
        this.infoTitle = (TextView)infoWindow.findViewById(R.id.title);
        this.infoSnippet = (TextView)infoWindow.findViewById(R.id.snippet);
        this.infoButton = (Button)infoWindow.findViewById(R.id.button);

        // Setting custom OnTouchListener which deals with the pressed state
        // so it shows up 
        this.infoButtonListener = new OnInfoWindowElemTouchListener(infoButton,
                getResources().getDrawable(R.drawable.btn_default_normal_holo_light),
                getResources().getDrawable(R.drawable.btn_default_pressed_holo_light)) 
        {
            @Override
            protected void onClickConfirmed(View v, Marker marker) {
                // Here we can perform some action triggered after clicking the button
                Toast.makeText(MainActivity.this, marker.getTitle() + "'s button clicked!", Toast.LENGTH_SHORT).show();
            }
        }; 
        this.infoButton.setOnTouchListener(infoButtonListener);


        map.setInfoWindowAdapter(new InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {
                // Setting up the infoWindow with current's marker info
                infoTitle.setText(marker.getTitle());
                infoSnippet.setText(marker.getSnippet());
                infoButtonListener.setMarker(marker);

                // We must call this to set the current marker and infoWindow references
                // to the MapWrapperLayout
                mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);
                return infoWindow;
            }
        });

        // Let's add a couple of markers
        map.addMarker(new MarkerOptions()
            .title("Prague")
            .snippet("Czech Republic")
            .position(new LatLng(50.08, 14.43)));

        map.addMarker(new MarkerOptions()
            .title("Paris")
            .snippet("France")
            .position(new LatLng(48.86,2.33)));

        map.addMarker(new MarkerOptions()
            .title("London")
            .snippet("United Kingdom")
            .position(new LatLng(51.51,-0.1)));
    }

    public static int getPixelsFromDp(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dp * scale + 0.5f);
    }
}

错误即将发生,因为谷歌已经删除了getMap,但不知道替代解决方案

错误:(149,42)错误:找不到符号方法getMap()


共 (2) 个答案

  1. # 2 楼答案

    您现有的代码是:

     final MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
        final GoogleMap map = mapFragment.getMap();
    

    试试这个而不是那个:

     MapFragment mapFragment; // declare in global scope
     GoogleMap googleMap; // declare in global scope
    

    将此代码放入OnCreate方法:

    mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
    
                    mapFragment.getMapAsync(new OnMapReadyCallback() {
                        @Override
                        public void onMapReady(GoogleMap map) {
                            googleMap = map;
    
                            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                            googleMap.setMyLocationEnabled(false); // false to disable
                            googleMap.setOnMarkerClickListener(onMarkerClick);
                            googleMap.setOnInfoWindowClickListener(onInfoWindowClick);
                            googleMap.getUiSettings().setZoomControlsEnabled(true);// Display Zoom Controls
                            googleMap.setMyLocationEnabled(true);// Display My Location Control
                        }
                    });
                }
    

    要将像素转换为DP,请使用以下命令:

        public int getPixelsFromDp(int px){ 
           DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics(); 
        return Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)); 
    }