3

我遇到問題SupportMapFragment並將相機移動到地圖上的某個位置。SupportMapFragment with ViewPager:無法移動相機或更改地圖設置

public class MapPositionFragment extends SupportMapFragment implements LocationListener{ 

private GoogleMap map = null; 
private Button lockButton = null; 
private Location currentLocation = null; 
private LocationManager locationManager = null; 
private View mapView = null; 

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    mapView = super.onCreateView(inflater, container, savedInstanceState); 
    View v = inflater.inflate(R.layout.fragment_route_map, container, false); 

    SetUpMap(v); 
    return v; 
    } 

@Override 
public void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
} 

@Override 
public void setUserVisibleHint(boolean isVisibleToUser) { 

    if (isVisibleToUser){ 
     SetUpLocationService(); 
    } 
} 

private void SetUpLockButton(){ 
    lockButton = (Button)getActivity().findViewById(R.id.lock_screen_button); 
    lockButton.setOnClickListener(new OnClickListener() {   
     public void onClick(View v) { 
      ToggleLockButton(); 
     } 
    });  
} 

private void ToggleLockButton(){ 
    BaseSwipeActivity baseActivity = (BaseSwipeActivity)getActivity(); 
    boolean swipeEnabled = baseActivity.TogglePaging(); 

    if (swipeEnabled){ 
     lockButton.setBackgroundResource(R.drawable.stock_lock_open); 
    } 
    else{ 
     lockButton.setBackgroundResource(R.drawable.stock_lock); 
    } 
} 

private void SetUpLocationService(){ 
    boolean gpsIsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    boolean networkIsEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

    if (networkIsEnabled){ 
     // Register the listener with the Location Manager to receive location updates 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
    } else if (gpsIsEnabled){ 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
    } 

} 

private void SetCurrentLocation(final Location location){ 

    if (map != null){ 
     getActivity().runOnUiThread(new Runnable() { 
       public void run() { 
       LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
       CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10); 
       map.moveCamera(cameraUpdate); 
       } 
     }); 

    } 
} 

private void SetUpMap(View view) { 
    if (map == null){ 
     getActivity().runOnUiThread(new Runnable() { 
       public void run() { 
       map = getMap(); 
       map.getUiSettings().setMyLocationButtonEnabled(true); 
       map.setIndoorEnabled(true); 
       map.getUiSettings().setZoomControlsEnabled(false); 
       locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
       } 
     }); 
    } 
} 

@Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    SetUpLockButton(); 
    } 


public void onLocationChanged(Location location) { 
     // Called when a new location is found by the network location provider. 
     SetCurrentLocation(location); 
} 

public void onProviderDisabled(String arg0) { 
    // TODO Auto-generated method stub 

} 

public void onProviderEnabled(String arg0) { 
    // TODO Auto-generated method stub 

} 

public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
    // TODO Auto-generated method stub 

} 

}

這是SupportMapFragment,其被定位在ViewPager內部。地圖顯示,但我無法更改地圖設置,我無法更改相機位置。我想也許是因爲它在另一個線程中,我不得不使用UI線程,但是這不會改變任何東西。

所以,也許有人有一個想法,這將是偉大的!

thx曼努埃爾

+0

這可能與'ViewPager'無關。檢查LogCat以查看授權是否失敗,在這種情況下,您的問題是您的清單中有錯誤的API密鑰,或者API密鑰未授權您在API控制檯中使用您的簽名密鑰或程序包名稱。下面是'ViewPager'中'SupportMapFragments'的示例項目:https://github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Pager – CommonsWare

+0

logcat中沒有錯誤。我的帖子的標題是錯誤的,我能夠顯示地圖。現在的問題是我無法改變地圖上的攝像頭位置。所以我的API鍵應該工作。 – Maniga

+0

在鏈接中查看我的示例代碼以更改相機位置。 – CommonsWare

回答

5

今天我遇到了同樣的問題。這是我的解決方案。

如您有:

// @ SetUpMap() 
map = getMap(); 

將其替換爲:

// Remeber to change the map id (R.id.map) to what you have in the layout .xml 
map = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)) 
     .getMap(); 


好像這個解決方案沒有與Android支持庫21.0.x工作(我已經測試過.2和.3)。由於此方法適用於較舊的Android支持庫,因此可能存在錯誤等。