如何将Google Maps V2显示为自定义对话框android

时间:2014-03-17 05:55:47

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

您好我的名字是Hernan所以我是Android开发人员。我提出这个问题是因为我需要将谷歌地图插入自定义对话框我已经让我的对话框正常工作但是当我添加谷歌地图时它会在代码中引发错误。这是我的代码。

Java代码:

package com.sigetdriver.view.popup;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.sigetdriver.R;

public class InsertarDestinoPopup {

    private GoogleMap googleMap;
    private Context context;
    public Dialog dialog;
    private Button btnCerrar;

    public void dialog(Context _context) {

        context = _context;
        dialog = new Dialog(_context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.popup_destino);
        dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

        try {
            // Loading map
            initilizeMap();
            pintarMapa();

        } catch (Exception e) {
            e.printStackTrace();
        }

        btnCerrar = (Button) dialog.findViewById(R.id.btnCerrar);
        btnCerrar.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });

        dialog.show();      

    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap(); // *******ERROR HERE******* 

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(context,
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    private void pintarMapa() {

        googleMap.setMyLocationEnabled(true); // false to disable

    }

}

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <Button
        android:id="@+id/btnCerrar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cerrar" />

</LinearLayout>

我不知道这是制作对话框或在对话框中显示地图的最佳方式,请提前解决此问题。

Hernan

2 个答案:

答案 0 :(得分:1)

您还可以将活动用作对话框。您可以执行所有常规活动方法。

XML编码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

Java编码

public class Map extends FragmentActivity {

private View rootView;

GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setBackgroundDrawable(new ColorDrawable(0));
    setContentView(R.layout.map);

    this.rootView=findViewById(R.id.map_view);

    FragmentManager myFragmentManager = getSupportFragmentManager();
    SupportMapFragment mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
    map = mySupportMapFragment.getMap();
}

@SuppressLint("NewApi")
@Override
public boolean onTouchEvent(MotionEvent event) {
        Rect rect = new Rect();
       rootView.getHitRect(rect);
       if (!rect.contains((int)event.getX(), (int)event.getY())){
        setFinishOnTouchOutside(false); 
           return true;
       }
       return false;       
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {

        finish();
    }
    return true;
}
}

清单文件

此处您将活动更改为对话框。

<activity
        android:name="com.example.mapindialog.Map"
        android:excludeFromRecents="true"
        android:launchMode="singleInstance"
        android:screenOrientation="landscape"
        android:theme="@android:style/Theme.Dialog" >
</activity>

答案 1 :(得分:0)

基于你的png,好像你正在使用支持v4包,在这种情况下你需要用getSupportFragmentManager替换getFragmentManager