2017-04-05 68 views
-1

我正在嘗試向我的應用內的Google地圖片段添加「我的位置」按鈕。這是到目前爲止我的代碼:將「我的位置」添加到Google地圖片段

package com.example.sander.app; 

公共類的GoogleMaps擴展片段實現OnMapReadyCallback {

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_gmaps, container, false); 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    MapView mapView = (MapView) view.findViewById(R.id.map); 
    mapView.onCreate(savedInstanceState); 

    mapView.onResume(); // needed to get the map to display immediately 

    try { 
     MapsInitializer.initialize(getActivity().getApplicationContext()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    mapView.getMapAsync(this); 

} 

@Override 
public void onMapReady(GoogleMap googleMap) { 

    LatLng marker = new LatLng(51.9244201, 4.4777325); 

    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 12)); 

    googleMap.addMarker(new MarkerOptions().title("Testing").position(new LatLng(53.92, 4.47))); 
    googleMap.addMarker(new MarkerOptions().title("Hello Google Maps!").position(marker)); 

    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    googleMap.setMyLocationEnabled(true); // This is how I had implemented the setMyLocationEnabled method 
    googleMap.getUiSettings().setMyLocationButtonEnabled(true); 

} 

}

我已經添加權限,以我的AndroidManifest.xml,和GoogleMap的。 setMyLocationEnable(真);不起作用。

誰能幫我到按鈕添加到我的谷歌地圖片段

+0

用於位置按鈕googleMap.setMyLocationEnabled(真);應該可以工作 –

+0

可以請你發佈你如何實現setMyLocationEnable(true)?您爲Manifest添加了什麼樣的權限? –

+0

我已經添加到代碼 –

回答

1

嘗試這個

mMap.setMyLocationEnabled(true); 
mMap.getUiSettings().setMyLocationButtonEnabled(true); 
+0

這是行不通的,因爲它不是一個活動。這是一個片段 –

+0

@SanderBakker它在我的片段中工作。它不需要活動只是把這個代碼放在'onMapReady(谷歌地圖谷歌地圖)' –

+0

'是的,我試過了,但之後,我得到了錯誤,因爲android的SDK 21和權限處理 –

0

請使用此代碼,如果有任何問題` 公共類的GoogleMaps擴展片段工具評論OnMapReadyCallback {

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_trip, container, false); 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    MapView mapView = (MapView) view.findViewById(R.id.fragment_map_trip); 
    mapView.onCreate(savedInstanceState); 

    mapView.onResume(); // needed to get the map to display immediately 

    try { 
     MapsInitializer.initialize(getActivity().getApplicationContext()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    mapView.getMapAsync(this); 

} 

@Override 
public void onMapReady(GoogleMap googleMap) { 

    LatLng marker = new LatLng(51.9244201, 4.4777325); 

    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 12)); 

    googleMap.addMarker(new MarkerOptions().title("Testing").position(new LatLng(53.92, 4.47))); 
    googleMap.addMarker(new MarkerOptions().title("Hello Google Maps!").position(marker)); 

    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    googleMap.setMyLocationEnabled(true); // This is how I had implemented the setMyLocationEnabled method 


} 

}` 對於佈局中的地圖使用此代碼

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/fragment_maps_frame" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:clickable="false"> 

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

代碼有效,但我看不到「我的位置」按鈕 –

+0

您正在使用我的代碼,或者您在代碼中添加了我的代碼? –

+0

我正在使用你的代碼。您可以在本主題頂部看到它(更新了代碼) –

相關問題