2016-03-02 100 views
2

你好,我已經實施SupportMapFragment谷歌地圖,並試圖設置我的位置按鈕和標記谷歌地圖(SupportMapFragment),但它不顯示,但此代碼在活動。谷歌地圖(SupportMapFragment)不顯示標記查看尋呼機碎片

請找到下面的代碼以獲取更多信息。

public class MapsFragment extends Fragment implements OnMapReadyCallback { 
    private static View view; 
    private SupportMapFragment mMap; 
    private static Double latitude, longitude; 
    GoogleMap gMap; 
    private static final int PERMISSION_REQUEST_CODE = 1; 

    public MapsFragment() { 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     view = inflater.inflate(R.layout.fragment_map, container, false); 
     latitude = 26.78; 
     longitude = 72.56; 
     FragmentManager fm = getChildFragmentManager(); 
     mMap = (SupportMapFragment) fm.findFragmentById(R.id.map); 
     if (mMap == null) { 
      mMap = SupportMapFragment.newInstance(); 
      fm.beginTransaction().replace(R.id.map, mMap).commit(); 
      mMap.getMapAsync(this); 
     } 
     return view; 
    } 

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

     gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new 
       LatLng(49.39, -124.83), 20)); 
     gMap.addMarker(new MarkerOptions() 
       .position(new LatLng(37.7750, 122.4183)) 
       .title("San Francisco") 
       .snippet("Population: 776733")); 
     gMap.getUiSettings().setZoomGesturesEnabled(true); 
     if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      gMap.setMyLocationEnabled(true); 
     } else { 
      requestPermission(); 
     } 

    } 
} 
+1

以此爲片段活動,而不是的片段。 –

+0

是地圖顯示? –

+0

是地圖顯示Bajirao Shinde –

回答

0

試試這個

public void drawMarker(double lat,double lon) 
{ 
    if (mMap != null) { 
     MarkerOptions marker = new MarkerOptions().position(new LatLng(lat, lon)).title(" Maps Tutorial").snippet("Android Ruler"); 
     marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); 
// Moving Camera to a Location with animation 
     CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(12).build(); 
     mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
     mMap.addMarker(marker); 
    } 
} 

參考更多詳細資料: - http://coderzpassion.com/android-google-maps-v2-tutorial-with-markers/

0

也許你還沒有權限當你使用setMyLocation();。試試這個 -

if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) 
      == PackageManager.PERMISSION_DENIED){ 
     FragmentCompat.requestPermissions(this, 
       new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
       PERMISSION_REQUEST); 
} 

手柄的結果 -

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if (requestCode == PERMISSION_REQUEST && grantResults.length > 0) { 
     if (permissions[0].equals(Manifest.permission.ACCESS_FINE_LOCATION)) { 
      if (grantResults[0] == PackageManager.PERMISSION_GRANTED){ 
       //Calling the function here 
       gMap.setMyLocationEnabled(true); 
      } 
      else 
       Toast.makeText(context, "You need to provide " + 
         "permission to find the location", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 
0

我有viewpager同樣的問題,我解決它使用OnMapLoaded回調。試着這樣做

public class MapsFragment extends Fragment implements OnMapReadyCallback, 
              GoogleMap.OnMapLoadedCallback { 

     @Override 
     public void onMapReady(GoogleMap map) { 
      gMap = map; 
      gMap.setOnMapLoadedCallback(this); 
     } 

     //map loaded callback 
     @Override 
     public void onMapLoaded() { 
      // add your marker now 
      gMap.addMarker(new MarkerOptions() 
       .position(new LatLng(37.7750, 122.4183)) 
       .title("San Francisco") 
       .snippet("Population: 776733")); 
     } 
} 
1

從這個

if **(mMap != null)** { 
      mMap = SupportMapFragment.newInstance(); 
      fm.beginTransaction().replace(R.id.map, mMap).commit(); 
      mMap.getMapAsync(this); 
     } 
+0

是的這是工作感謝Pankaj Sharma –

1
import com.google.android.gms.maps.SupportMapFragment; 

SupportMapFragment mMapFragment = SupportMapFragment.newInstance();  
FragmentManager fm = fragment.getChildFragmentManager(); 
fm.beginTransaction().add(R.id.map, mMapFragment).commit(); 
mMapFragment.getMapAsync(this); 

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

if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), 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; 
} 
mGoogleMap.setMyLocationEnabled(true); 

buildGoogleApiClient(); 

mGoogleApiClient.connect(); 
} 

protected synchronized void buildGoogleApiClient() { 

mGoogleApiClient = new GoogleApiClient.Builder(getContext()) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .addApi(LocationServices.API) 
     .build(); 
} 

@Override 
public void onConnected(Bundle bundle) { 

if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), 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; 
} 
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
     mGoogleApiClient); 
if (mLastLocation != null) { 
    //place marker at current position 
    mGoogleMap.clear(); 
    //setLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude()); 
} 


mLocationRequest = new LocationRequest(); 
mLocationRequest.setInterval(50000); //50 seconds 
mLocationRequest.setFastestInterval(30000); //30 seconds 
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
//mLocationRequest.setSmallestDisplacement(0.1F); //1/10 meter 

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
} 

@Override 
public void onConnectionSuspended(int i) { 
Toast.makeText(customAdapter.getContext(), "onConnectionSuspended", Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
Toast.makeText(customAdapter.getContext(), "onConnectionFailed", Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onLocationChanged(Location location) { 
sourceLat = location.getLatitude(); 
sourceLng = location.getLongitude(); 
getRestaurantDetails(); 
latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
//latLng = new LatLng(lat, lng); 
//Log.wtf("Lat lng", lat + " " + lng); 
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18)); 
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
} 

替換您的代碼,其中片段是你viewPagerFragment實例

在XML

<LinearLayout 
android:id="@+id/map" 
android:layout_width="match_parent" 
android:layout_height="250dp" />