2013-11-26 121 views
1

下面的代碼應該追蹤用戶的當前位置,但是「updatePlaces(this);」給我錯誤「MainActivity.CustomMapFragment類型的方法updatePlaces(Context)不適用於參數(MainActivity.CustomMapFragment)」。我對這一切還是比較陌生的,而且我無法理解這個錯誤。我怎麼能解決它?「類型中的方法不適用於參數」錯誤

/** 
* A fragment that displays the map. 
*/ 
public static class CustomMapFragment extends Fragment implements 
     LocationListener { 

    private MapView mapView; 
    private GoogleMap map; 
    private int userIcon, AedValidatedIcon, AedNotValidatedIcon; 
    private LocationManager locMan; 
    private Marker userMarker; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     // Icons 
     userIcon = R.drawable.blue_point; 
     AedValidatedIcon = R.drawable.yellow_point; 
     AedNotValidatedIcon = R.drawable.red_point; 

     // Inflate and return the layout 
     View v = inflater.inflate(R.layout.map_fragment, container, false); 
     mapView = (MapView) v.findViewById(R.id.mapView); 
     mapView.onCreate(savedInstanceState); 
     mapView.onResume();// needed to get the map to display immediately 

     map = mapView.getMap(); 
     map.getUiSettings().setAllGesturesEnabled(false); 
     map.getUiSettings().setZoomControlsEnabled(false); 
     map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
     updatePlaces(this); 

     return v; 
    } 

    private void updatePlaces(Context c) 
    { 
     locMan = (LocationManager)c.getSystemService(LOCATION_SERVICE); 
     Location lastLoc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

     double lat = lastLoc.getLatitude(); 
     double lng = lastLoc.getLongitude(); 

     LatLng lastLatLng = new LatLng(lat, lng); 

     if (userMarker!=null) userMarker.remove(); 

     userMarker = map.addMarker(new MarkerOptions() 
      .position(lastLatLng) 
      .title("You are here") 
      .icon(BitmapDescriptorFactory.fromResource(userIcon)) 
      .snippet("Your last recorder location")); 

     map.animateCamera(CameraUpdateFactory.newLatLng(lastLatLng), 3000, null); 
     locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30000, 50, this); 
    } 

回答

3

更換

updatePlaces(this); 

updatePlaces(getActivity()); 

public final Activity getActivity()

返回的Activity這個fragment當前關聯。

+0

非常感謝!它現在的作品:) – Berry

+0

@BerryHermans歡迎您 – Raghunandan

1

由於Fragment是不是你需要調用Activity這個Fragment與這樣相關的Context

updatePlaces(this.getActivity()); 
+0

謝謝!它現在工作:) – Berry

相關問題