Reputation:
I'm trying to implement a navigation Drawer which has a Map in a Fragment. Here is my code.
Here is the fragment_map.xml
<FrameLayout 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" tools:context="com.myayubo.FragmentMap">
<!-- TODO: Update blank fragment layout -->
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btnCity"
/>
</FrameLayout>
Then I called FragmentMap in my ExtractMenu.java
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment = null;
switch (position){
case 0:
fragment = new FragmentMap();
break;
}
if (fragment != null){
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
}
}
Then in FragmentMap.java
following code was added to Zoom the ,ap to current location.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mMap.setMyLocationEnabled(true); // Identify the current location of the device
mMap.setOnMyLocationChangeListener(this); // change the place when the device is moving
Location currentLocation = getMyLocation(); // Calling the getMyLocation method
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_map, container, false);
}
// Implement getMyLocation
public Location getMyLocation() {
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); // Get location from GPS if it's available
Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// Location wasn't found, check the next most accurate place for the current location
if (myLocation == null) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
// Finds a provider that matches the criteria
String provider = lm.getBestProvider(criteria, true);
// Use the provider to get the last known location
myLocation = lm.getLastKnownLocation(provider);
}
return myLocation;
}
Here I cannot resolve getSystemService
Any one knows why? Is there any better way to Zoom the Map in Fragment?
Upvotes: 4
Views: 8405
Reputation: 170
Use this
LocationManager lm = LocationManager.getActivity().getSystemService(Context.LOCATION_SERVICE);
Upvotes: 0
Reputation: 75788
getActivity() in a Fragment returns the Activity the Fragment is currently associated with.
You are using Fragment, So you need to add getActivity()
.I hope it will helps you .
Finally ,
LocationManager lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
Upvotes: 10
Reputation: 35579
you need to use
For Fragment
:
getActivity().getSystemService()
For Activity
:
getSystemService()
so for your code it will be
LocationManager lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
Upvotes: 0