将getMap更改为GetMapAsync

时间:2018-09-25 20:12:53

标签: java android google-maps-android-api-2

我正在尝试基于Stack Overflow的答案之一创建一个交互式infoWindow。 但是,我在代码中使用getMapAsync()时遇到了错误。尽管我尝试使用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); } } ,但无法解决问题。请帮我。如果带有按钮的交互式信息窗口有新代码可用,请共享代码。

getMap()

由于Google已删除了=IF(AND(B3<>0, B3<>A3), E3*D3*B3, E3*D3*A3) ,因此出现了错误,但是我不知道该方法的替代解决方案。

1 个答案:

答案 0 :(得分:0)

更新

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

onMapReady中,您应该写此行 gMap = googleMap,然后您就可以在onMapReady

中进行所需的操作
  • 获取当前位置
  • 地图的自定义设置
  • 和...

现在是时候在您的OnCreate方法中使用这些行了

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

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