gpt4 book ai didi

java - 我如何将 Google Maps V2 显示到自定义对话框 android

转载 作者:行者123 更新时间:2023-11-30 03:01:32 25 4
gpt4 key购买 nike

大家好,我叫 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>

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

埃尔南

最佳答案

你也可以把activity当作一个dialog来使用。这里你可以做所有正常的activity方法。

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;
}
}

list 文件

在这里,您将 Activity 更改为对话。

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

关于java - 我如何将 Google Maps V2 显示到自定义对话框 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22447823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com