2016-05-25 95 views
0

我是Android新手,完全不熟悉Android中的Google地图实施。我有一个应该显示Google地图的片段,但是我希望它默认打开奥斯陆城市的地图。我尝试了几件事,但没有奏效。它只是显示了非洲附近的世界地图。Google Map Fragment默认位置

这里是我的代码:

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> 
+0

布局MapView类的ID MapView类,但在代码R.id.map,它们必须是相同的 – Blackkara

+0

使用这些代码 59.977988 ,10.606613 – Dharmaraj

回答

0

试试这个,

在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)); 
+0

我纠正了这个id,并试了你的代码。它仍然不起作用 – Edd

+0

不,它不工作。再次显示世界地图。 – Edd

+0

事实上,它现在正在工作......非常感谢... – Edd