2015-07-28 56 views
0

我有使用GoogleMaps API的應用程序,並且遇到了點擊myLocationButton時無法執行任何操作的問題。所以按鈕是可見的,我看到了我的位置,但是當我點擊按鈕時,它不會將攝像機對準y位置。這是我的代碼:GoogleMaps Android API我的位置問題

1)片段地圖:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    setRetainInstance(true); 
    mapView = (MapView) v.findViewById(R.id.mapview); 
    mapView.onCreate(null); 
    MapsInitializer.initialize(this.getActivity()); 

    mMap = mapView.getMap(); 
    mMap.setMyLocationEnabled(true); 

    UISettings = mMap.getUiSettings(); 
    UISettings.setMapToolbarEnabled(false); 
    UISettings.setZoomControlsEnabled(true); 
} 

2)XML地圖:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:slidingLayer="http://schemas.android.com/apk/res-auto"> 

<com.google.android.gms.maps.MapView android:id="@+id/mapview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 
+0

CameraUpdate zoom = CameraUpdateFactory.zoomTo(15); map.moveCamera(center); map.animateCamera(zoom);使用此 –

+0

有時它不會移動,直到它獲得一個準確的位置,然後自動移動相機。 – EdgarMarcoPolo

回答

2

我想,你可以用下面的代碼,我已經使用,它的工作原理細:

1)帶有地圖的片段:

MapView mapView;

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.mapview, container, false); 
    // Gets the MapView from the XML layout and creates it 

    MapsInitializer.initialize(getActivity()); 


    switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) { 
     case ConnectionResult.SUCCESS: 
      Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show(); 
      mapView = (MapView) v.findViewById(R.id.map); 
      mapView.onCreate(savedInstanceState); 
      // Gets to GoogleMap from the MapView and does initialization stuff 
      if (mapView != null) { 
       mapView.getMapAsync(new OnMapReadyCallback() { 
        @Override 
        public void onMapReady(GoogleMap googleMap) { 
         googleMap.getUiSettings().setMyLocationButtonEnabled(true); 
         googleMap.setMyLocationEnabled(true); 
         CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10); 
         googleMap.animateCamera(cameraUpdate); 
        } 
       }); 

      } 
      break; 
     case ConnectionResult.SERVICE_MISSING: 
      Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show(); 
      break; 
     case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED: 
      Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show(); 
      break; 
     default: 
      Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show(); 
    } 

    return v; 
} 

2)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/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:name=".YOUR_FRAGMENT_NAME_HERE"/> 

</RelativeLayout> 

請注意,您需要更改android:name=".YOUR_FRAGMENT_NAME_HERE"你的名字片段以上。在這一點上,你應該很好:)

+0

謝謝,但它有效,但我們有MapView。關鍵是我試圖使用片段,並且有一個例外... – sttimchenko

+0

我提供的代碼是用於片段,您可以將它嵌入到片段中。你得到什麼樣的例外? – bjiang

+0

我在mMap的getMap()後得到NullPointer。 – sttimchenko