【问题标题】:Google Map Options Android谷歌地图选项安卓
【发布时间】:2015-03-18 15:30:43
【问题描述】:

我想在我的 Android 应用程序中向 Google 地图片段添加属性。我是否正确找到了地图?我将如何实现下面的示例?源代码如下。

示例(我想添加类似的内容)

@Override
public void onMapReady(GoogleMap map) {
    map.addMarker(new MarkerOptions()
        .position(new LatLng(0, 0))
        .title("Marker"));
}

fragment1_layout.xml

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

     <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:text="@string/textView6"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />

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

    <Button
        android:id="@+id/btnRespond"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="@string/btnRespond"
        android:layout_gravity="center"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        android:background="@drawable/button_animation"
        android:padding="5dp"/>


</LinearLayout>

Fragment1.java

package ie.itsligo.medication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class Fragment1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment1_layout, container, false);

        Fragment map = (Fragment) getFragmentManager().findFragmentById(R.id.map);

        Button btn = (Button) view.findViewById(R.id.btnRespond);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "You Are Now Active! Swipe Left For Individual Details & Medical Information", Toast.LENGTH_LONG).show();
            }
        });

        return view; 
    }
}

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    在您的onCreateView 方法中将您的fragment 转换为MapFragment,然后像这样调用您的地图异步

    MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    

    取自谷歌地图文档here

    然后OnMapReady会在地图加载时自动调用。

    【讨论】:

    • get this error = "Cannot cast from Fragment to MapFragment"
    • 尝试将getFragmentManager()更改为getSupportFragmentManager()
    • 新错误 = "方法 getSupportFragmentManager() 未定义类型 Fragment1"
    • 先试试这个,用getActivity().getSupportFragmentManager()替换它,如果这不起作用检查this question,我相信它正在尝试做同样的事情。
    • 是因为我使用 onCreateView 而不是 onCreate 因为当我尝试通过 id "txt1 = (TextView) getView().findViewById(R.id.txt1) 查找 textview 时会发生同样的事情;"而不是“txt1 = (TextView) findViewById(R.id.txt1);”因为我在 findViewById 部分收到错误
    猜你喜欢
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多