谷歌地图片段默认位置

时间:2016-05-25 19:56:30

标签: android

我是Android新手,完全不熟悉Android中的Google地图实施。我有一个应该显示谷歌地图的片段,它确实如此,但我希望它默认打开奥斯陆市的地图。我尝试过一些东西,但没有任何效果。它只是显示非洲附近的世界地图。

这是我的代码:

MapFragment.java

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapFragment extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map_fragment);
    ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
            .getMapAsync(this);
}

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

private void setUpMap() {
    LatLng latLng = new LatLng(59.95, 10.75);

    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    mMap.animateCamera(CameraUpdateFactory.zoomTo(14));

}
}

activity_map_fragment.xml

<?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"
    class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

试试这个,

在xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="match_parent">
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>

在代码中,

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView);
mapFragment.getMapAsync(this);

最后,对于相机

LatLng latLng = new LatLng(59.95, 10.75);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17));
相关问题