【问题标题】:Google maps in my custom activity我的自定义活动中的谷歌地图
【发布时间】:2017-05-31 11:14:02
【问题描述】:

我正在尝试集成 Google 地图以显示我的活动中的特定位置,ResortDetails 以显示我从数据库中收到的值的纬度和经度,但我无法这样做。这是我的java代码:

public class ResortDetails extends AppCompatActivity implements  View.OnClickListener, OnMapReadyCallback{

private GoogleMap mMap;
protected void onCreate(Bundle savedInstanceState) {
       try {
        // Loading map
        initializeMap();

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

 private void initializeMap() {
    if (mMap == null) {
        SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFrag.getMapAsync(this);

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

MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title(res_name);
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(12).build();

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    setUpMap();
}

public void setUpMap(){

    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    mMap.addMarker(marker);
    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    //mMap.setMyLocationEnabled(true);
    mMap.setTrafficEnabled(true);
    mMap.setIndoorEnabled(true);
    mMap.setBuildingsEnabled(false);
    mMap.getUiSettings().setZoomControlsEnabled(true);
}

它总是以吐司结束:Sorry! unable to create maps

我的 XML 文件在为片段选择布局时出错,所以我这样做了。这是 ResortDetails 的 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1"
app:layout_collapseParallaxMultiplier="1.0"
app:layout_scrollFlags="scroll">

<TextView
    android:id="@+id/res_n"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal|center"
    android:gravity="top|center_vertical|center_horizontal"
    android:textSize="28sp" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1.00">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="10dp"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="245dp"
            android:layout_gravity="center_vertical|center_horizontal"
            android:padding="20dp"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="15dp"
            tools:context="com.tripoffbeat.ResortDetails"
            tools:layout="@layout/activity_maps" />


    </LinearLayout>
</ScrollView>

<Button
    android:id="@+id/back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    android:padding="10dp"
    android:paddingBottom="10dp"
    android:paddingEnd="10dp"
    android:paddingStart="10dp"
    android:paddingTop="10dp"
    android:text="Back" />

</LinearLayout>`

我使用@layout/activity_maps 作为片段的布局,其中包含一个地图视图。

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

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

我不太确定我选择的布局是否正确,或者我制作它的方式是否正确。请帮帮我。

【问题讨论】:

  • 尝试使用我的朋友的地图活动,如果有任何疑问,最好使用询问我
  • 但我希望在我自己的活动中使用地图,而不是在地图活动预设中。 @NileshRathod
  • 如有任何疑问请咨询我
  • 您在地图尚未创建时显示 Toast?

标签: java android google-maps android-fragments


【解决方案1】:

在 Google for Maps 的最新更新中,片段仅支持 MapView。 MapFragment 和 SupportMapFragment 不起作用。我可能错了,但这是我在尝试实现 MapFragment 和 SupportMapFragment 后看到的。

设置显示地图的布局。 location_fragment.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
 </RelativeLayout>

现在我们编写用于显示地图的 java 类。 MapViewFragment

public class MapViewFragment extends Fragment {

MapView mMapView;
private GoogleMap googleMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.location_fragment, container, false);

    mMapView = (MapView) rootView.findViewById(R.id.mapView);
    mMapView.onCreate(savedInstanceState);

    mMapView.onResume(); // needed to get the map to display immediately

    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }

    mMapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap mMap) {
            googleMap = mMap;

            // For showing a move to my location button
            googleMap.setMyLocationEnabled(true);

            // For dropping a marker at a point on the Map
            LatLng sydney = new LatLng(-34, 151);
            googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));

            // For zooming automatically to the location of the marker
            CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        }
    });

    return rootView;
}

@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mMapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}

}

最后,您需要通过在Google Cloud Console 注册您的应用程序来获取您的应用程序的 API 密钥。将您的应用注册为原生 Android 应用

【讨论】:

  • 这样做之后,我是在我的 ResortDetails 类中包含 MapViewFragment 的一个对象,还是直接在我的 ResortDetails 类中实现地图? @Nilesh
  • 通过使用此代码,您可以在任何 ViewPager 或 Fragment 或 Activity 内的任何地方设置 MapView
  • 在 Google for Maps 的最新更新中,片段仅支持 MapView。 您从哪里看到的? MapFragment & SupportMapFragment不起作用 你是怎么得到这个的 我可能错了,但这是我在尝试实现 MapFragment 和 SupportMapFragment 后看到的。 是的,你肯定错了
猜你喜欢
  • 1970-01-01
  • 2020-06-04
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 2012-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多