【发布时间】: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
-
点击此链接 :-> stackoverflow.com/questions/18206615/…
-
如有任何疑问请咨询我
-
您在地图尚未创建时显示 Toast?
标签: java android google-maps android-fragments