Java 将getMap更改为GetMapAsync

Java 将getMap更改为GetMapAsync,java,android,google-maps-android-api-2,Java,Android,Google Maps Android Api 2,我试图根据Stack Overflow的一个答案创建一个交互式信息窗口。 但是,代码中使用的getMap()有一个错误。虽然我尝试了getmapsync(),但无法解决该问题。请帮帮我。如果有任何新代码可用于带有按钮的交互式信息窗口,请共享该代码 public class MainActivity extends AppCompatActivity { private ViewGroup infoWindow; private TextView infoTitle; private TextV

我试图根据Stack Overflow的一个答案创建一个交互式信息窗口。 但是,代码中使用的
getMap()
有一个错误。虽然我尝试了
getmapsync()
,但无法解决该问题。请帮帮我。如果有任何新代码可用于带有按钮的交互式信息窗口,请共享该代码

public class MainActivity extends AppCompatActivity {

private ViewGroup infoWindow;
private TextView infoTitle;
private TextView infoSnippet;
private Button infoButton1, infoButton2;
private OnInfoWindowElemTouchListener infoButtonListener;

static final LatLng latlng1 = new LatLng(28.5355, 77.3910);
static final LatLng latlng2 = new LatLng(28.6208768, 77.3726377);

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

    final SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
    final MapWrapperLayout mapWrapperLayout = (MapWrapperLayout)findViewById(R.id.map_relative_layout);
    final GoogleMap googleMap = mapFragment.getMap();

    // MapWrapperLayout initialization
    // 39 - default marker height
    // 20 - offset between the default InfoWindow bottom edge and it's content bottom edge
    mapWrapperLayout.init(googleMap, 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.custom_infowindow, null);

    this.infoTitle = (TextView)infoWindow.findViewById(R.id.nameTxt);
    this.infoSnippet = (TextView)infoWindow.findViewById(R.id.addressTxt);
    this.infoButton1 = (Button)infoWindow.findViewById(R.id.btnOne);
    this.infoButton2 = (Button)infoWindow.findViewById(R.id.btnTwo);

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

    infoButtonListener = new OnInfoWindowElemTouchListener(infoButton2, getResources().getDrawable(R.drawable.btn_bg),getResources().getDrawable(R.drawable.btn_bg)){
        @Override
        protected void onClickConfirmed(View v, Marker marker) {
            Toast.makeText(getApplicationContext(), "click on button 2", Toast.LENGTH_LONG).show();
        }
    };
    infoButton2.setOnTouchListener(infoButtonListener);

    /*infoWindow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "click on infowindow", Toast.LENGTH_LONG).show();
        }
    });*/

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

        @Override
        public View getInfoContents(Marker marker) {
            // Setting up the infoWindow with current's marker info
            infoSnippet.setText(marker.getTitle());
            infoTitle.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
    googleMap.addMarker(new MarkerOptions()
            .position(latlng1)
            .title("Source")
            .snippet("Comapny Name")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));

    googleMap.addMarker(new MarkerOptions()
            .position(latlng2)
            .title("Destination")
            .snippet("AmisunXXXXXX")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));

    //googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15));
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng1, 10));

}

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

}

出现错误是因为Google已经删除了
getMap()
,但我不知道有什么替代方案。

更新

在您的主要活动中定义
private GoogleMap gMap
并实施
OnMapReadyCallback
并实施
onMapReady
方法

    SupportMapFragment supportMapFragment = (SupportMapFragment)
            getSupportFragmentManager().findFragmentById(R.id.map);
    supportMapFragment.getMapAsync(this);
onMapReady
中,您应该写这一行
gMap=googleMap
,那么你就可以在
onMapReady中做你想做的事了

  • 获取当前位置
  • 自定义地图设置
  • 而且
现在是在
OnCreate
方法中使用这些行的时候了

    SupportMapFragment supportMapFragment = (SupportMapFragment)
            getSupportFragmentManager().findFragmentById(R.id.map);
    supportMapFragment.getMapAsync(this);

它应该可以解决您的问题,希望它能帮助您

我要求我在4月份实现,您能提供更多的代码吗?