2017-02-16 38 views
0

我想獲取用戶在地圖中的當前位置我無法在mapAsync函數中將「this」作爲參數傳遞 我在getMapAsync(this)中收到錯誤。它告訴不兼容的類型。無法解析片段中的getMapAsync

需要com.google.android.gms.map.GoogleMap

發現void how can I solve this issue ?

我也試過getActivity.getApplicationContext(),但它也沒有工作! 我在這裏提供的代碼,請檢查一下

public class MapsActivity extends Fragment implements 
    OnMapReadyCallback, 
    View.OnClickListener , 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    LocationListener { 

private GoogleMap map; 
private MapView mapView; 
private boolean mapsSupported = true; 
private GoogleApiClient mGoogleApiClient; 
MapFragment mapFragment; 
@Override 
public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    MapsInitializer.initialize(getActivity()); 

    if (mapView != null) { 
     mapView.onCreate(savedInstanceState); 
    } 
    initializeMap(); 
} 
private void initializeMap() { 
    SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    //mapFragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map); 
    if (map == null) { 
     **map = mapFragment.getMapAsync(this);** 
    } 
    // Enable Zoom 
    map.getUiSettings().setZoomGesturesEnabled(true); 
    //set Map TYPE 
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    //enable Current location Button 
    map.setMyLocationEnabled(true); 
    LocationManager locationManager = (LocationManager)getActivity().getSystemService(getActivity().LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    String bestProvider = locationManager.getBestProvider(criteria, true); 
    if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return; 
    } 
    Location location = locationManager.getLastKnownLocation(bestProvider); 
    if (location != null) { 
     onLocationChanged(location); 
    } 
    locationManager.requestLocationUpdates(bestProvider, 2000, 0, this); 
} 

Maps.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/map" 
tools:context=".MapsActivity" 
android:name="com.google.android.gms.maps.SupportMapFragment" 
tools:layout="@layout/abc_action_menu_layout"/> 

<Button 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Nearby Resturants" 
    android:id="@+id/btn_rest" 
    android:layout_gravity="bottom" /> 


    </RelativeLayout> 
+0

你可以使用** getMapAsync(getActivity()); ** – pulkit

+0

@pulkit不起作用 – SFAH

+0

爲使'getMapAsync()'工作,'R.id.map'必須是'MapFragment'元素。但它是一個正常的片段 – ShahiM

回答

0

試試這個代碼

try { 
     if (map == null) { 
      ((MapView) getView().findViewById(R.id.map)).getMapAsync(this); 

     } 
    }catch (NullPointerException e){ 
     e.printStackTrace(); 
    } 

希望這有助於!

+0

謝謝它的工作! :D @泰米德拉赫曼 – SFAH

+0

很高興知道!請將其標記爲已接受(答案左側的勾號標記),以便將來可以幫助其他開發人員。 –

2

調用內部FragmentonCreateView()

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

    View viewOBJ= inflater.inflate(R.layout.xxx, null, false); 

    SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
    return viewOBJ; 
} 

代碼欲瞭解更多信息,請閱讀getMapAsync() in Fragment

+0

我試過但不工作:/ – SFAH

0

你缺少的是getMapAsync是一個異步功能。這意味着它不會立即返回地圖對象。所以你不能只寫map = mapFragment.getMapAsync();

相反,你必須這樣做:

private void initializeMap() { 
    SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager() 
     .findFragmentById(R.id.map); 
    if (map == null) { 
    mapFragment.getMapAsync(this); 
    } 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    map = googleMap; 

    // Enable Zoom 
    map.getUiSettings().setZoomGesturesEnabled(true); 
    //set Map TYPE 
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    //enable Current location Button 
    map.setMyLocationEnabled(true); 

    //... rest of your map related code. 
} 

這裏,onMapReady時調用地圖對象的可用的,所以你的初始化代碼的其餘部分將去那裏。您還必須實施OnMapReadyCallback,但我知道您已經完成了這項工作。