弹出框内的Google地图视图

时间:2018-03-03 16:00:19

标签: android google-maps

我试图在弹出框中找到谷歌地图视图。我已经创建了弹出框,它们按预期工作。我已经搜遍了所有找到这个问题的解决方案,但我找不到任何东西。

非常感谢任何建议

弹出框     class pop2扩展了Activity {

@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密钥。

2 个答案:

答案 0 :(得分:0)

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

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>

看我的截图.... Screenshot

答案 1 :(得分:0)

           @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();