0

I tried so many different solutions and I also searched on Google but still not getting the required solution. The problem is not yet fixed. I have ViewPager with some Fragments. In first Fragment I wanna use Google Map API v2 to show current location of device. It works fine if I use current values of latitude/longitude and zoom it. Whats the best way to know current latitude and longitude of current location in Fragment (NOT FragmentActivity or Activity)? I just want to know is it possible in Fragment? Let me know please if someone had same problem and solved it!

Thanks for any help!

Here below code that I used to set map:

fragment.xml

<RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/map_container">

      <!-- The map fragments will go here -->
</RelativeLayout>

MainFragment.java

public class MainFragment extends Fragment{
      private SupportMapFragment mSupportMapFragment;
      private GoogleMap mGoogleMap;

      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        FragmentManager mFragmentManager = getChildFragmentManager();
        mSupportMapFragment = (SupportMapFragment) mFragmentManager.findFragmentById(R.id.map_container);
        if (mSupportMapFragment == null) {
            mSupportMapFragment = SupportMapFragment.newInstance();
            mFragmentManager.beginTransaction().replace(R.id.map_container, mSupportMapFragment).commit();
        }
    }

    @Override
    public void onResume(){
    if(mGoogleMap==null){
            mGoogleMap = mSupportMapFragment.getMap();
            //Set the marker
            mGoogleMap.addMarker(new MarkerOptions()
                    .position(new LatLng(???))
                    .title("Location")
                    .snippet("Your current location"));

            // Move the camera and zoom it to location
            mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(???), 15));
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193

1 Answers1

0

for current Location..Try with this..

    LocationManager lm;
    Location location;
    Geocoder geo;

    //convert lat, long to address.
    List<Address> addresses;
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    //get the last known latitude longitude from network
    location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    //create an instance of geocoder
    geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
    try
    {
        addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        if(addresses.get(0).getPostalCode() != null)
        {
            //You can get Lat, Long or Zip code
        }
    }
    catch (Exception e)
    {

    }
Amitabha Biswas
  • 3,281
  • 1
  • 11
  • 23
  • 1
    Hey man, is it joke? Check my post carefully. It seems to me you dont understand my question. I want to know current location of device but all materials that I found use Activity or FragmentActivity which dont work correctly in Fragment. So I want to know how correctly do that in my case. Do you have any ideas? – Nurzhan Nogerbek Feb 14 '15 at 10:20
  • Hello again! I look at you updated code and I have some questions. How do you think is it better to check connection with service before use it? And where is better to use your code? – Nurzhan Nogerbek Feb 14 '15 at 10:53