弹出框内的谷歌地图视图



我正在尝试在弹出框中获取谷歌地图视图。我已经创建了弹出框,它们正在按预期工作。我已经四处寻找这个问题的解决方案,但我找不到任何东西。

任何建议将不胜感激

弹出框 类 pop2 扩展活动 {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.popwindow2);
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    getWindow().setLayout((int)(width*.8),(int)(height*.6));
}
}

上面的代码在屏幕上将一个空活动显示为弹出框。我为其附加了一个自定义主题,以使未被框填充的屏幕部分透明。我正在寻找一种方法,用地图使整个活动充满。我已经设置了 API 密钥。

           @SuppressLint("ValidFragment")
       public class MapDialog extends DialogFragment
        implements OnMapReadyCallback{
@BindView(R.id.progressBar)ProgressBar progressBar;
@BindView(R.id.Locality) TextView mLocationMarkerText;
private GoogleApiClient googleApiClient;
private SupportMapFragment mapFragment;
private GpsTest gpsTest;
private GoogleMap googleMap;
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
public static final int ADDRESS_READER_PERMISSION_REQUEST = 201;
private static final int REQUEST_CODE_AUTOCOMPLETE = 1;
private Marker marker;
private Location location;
private double latitude;
private double longitude;

 @SuppressLint("ValidFragment")
 public MapDialog() {
    mapFragment = new SupportMapFragment();
 }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
  Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.map_alert_dialog,container);
    ButterKnife.bind(this,view);
    getDialog().setTitle("");
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
    setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme);
     googleApiClient = new 
  GoogleApiClient.Builder(getActivity()).addApi(AppIndex.API).build();
    if (FunctionUtil.isLocationEnabled(getActivity())) {
        FragmentTransaction transaction = 
    getChildFragmentManager().beginTransaction();
        transaction.add(R.id.mapView, mapFragment).commit();
        mapFragment.getMapAsync(this);
        if (checkPlayServices()) {
            buildGoogleApiClient();
        }
        } 
return view;
}
@Override
public void onMapReady(final GoogleMap googleMap) {
    this.googleMap = googleMap;
    if (ActivityCompat.checkSelfPermission(getActivity(), 
    Manifest.permission.ACCESS_FINE_LOCATION) != 
     PackageManager.PERMISSION_GRANTED && 
      ActivityCompat.checkSelfPermission(getActivity(), 
       Manifest.permission.ACCESS_COARSE_LOCATION) != 
         PackageManager.PERMISSION_GRANTED) {
        return;
    }
    googleMap.setMyLocationEnabled(true);
    googleMap.getUiSettings().setMapToolbarEnabled(false);
    googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() 
      {
        @Override
        public void onMapClick(LatLng latLng) {
            if (marker != null) {
                marker.remove();
            }
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            getAddress(latLng.latitude, latLng.longitude);
            marker = googleMap.addMarker(markerOptions);
            marker.setTitle(getAddress1(latLng.latitude, 
           latLng.longitude));
   //googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 
         10.0f));
        }
    });
}

  @Override
    public void onStart() {
    super.onStart();
    googleApiClient.connect();
    }
   @Override
   public void onStop() {
    super.onStop();
    googleApiClient.disconnect();
    }
protected synchronized void buildGoogleApiClient() {
    googleApiClient = new GoogleApiClient.Builder(getActivity())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

private boolean checkPlayServices() {
    int resultCode = 
  GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
    if (resultCode != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) 
           {
            GooglePlayServicesUtil.getErrorDialog(resultCode, 
                   getActivity(),
                    PLAY_SERVICES_RESOLUTION_REQUEST).show();
        } else {
        }
        return false;
    }
    return true;
}

 }
       map_alert_dialog.xml
 ?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light_gray"
android:minHeight="@dimen/dialog_min_height"
android:minWidth="1000dp"
android:orientation="vertical">
    <fragment
        android:id="@+id/place_autocomplete_fragment"
        android:layout_width="match_parent"
        android:layout_height="50dp"
       android:name=
 "com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        />
    <FrameLayout
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginBottom="@dimen/space_medium"
        android:layout_marginTop="@dimen/space_medium"
        android:minHeight="300dp">
   </FrameLayout>
 </LinearLayout>
      for open popup call like .
      MapDialog m = new MapDialog().show();

在布局中添加地图片段。然后在您的活动中找到它,并用它做任何您想做的事情。

public class MapActivity extends FragmentActivity implements OnMapReadyCallback 
{
private GoogleMap mMap;
double lat, lng;
String location;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    lat=getIntent().getDoubleExtra("lat",-34);
    lng=getIntent().getDoubleExtra("lng", 151);
    location=getIntent().getStringExtra("location");
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    DisplayMetrics dm=new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width=dm.widthPixels;
    int height=dm.heightPixels;
    getWindow().setLayout((int)(width*.85),(int)(height*.65));

}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    // Add a marker in Sydney, Australia, and move the camera.
    LatLng myLocation = new LatLng(lat, lng);
    mMap.addMarker(new MarkerOptions().position(myLocation).title(location));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myLocation,7));
}
}

这是我的 xml 代码....

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.instantdebate.speechtotext.MapActivity">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context=".MapsActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>

看到我的截图....截图

最新更新