【问题标题】:Show current location of device Google Map (Fragment)显示设备谷歌地图的当前位置(片段)
【发布时间】:2015-02-14 08:35:05
【问题描述】:

我尝试了很多不同的解决方案,我也在谷歌上搜索过,但仍然没有得到所需的解决方案。问题还没有解决。我有一些片段的 ViewPager。在第一个片段中,我想使用 Google Map API v2 来显示设备的当前位置。如果我使用当前的纬度/经度值并缩放它,它工作正常。了解Fragment 中当前位置NOT FragmentActivity 或Activity)的当前纬度和经度的最佳方法是什么?我只想知道片段中是否有可能?如果有人遇到同样的问题并解决了,请告诉我!

感谢您的帮助!

下面是我用来设置地图的代码:

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);
        }
    }
}

【问题讨论】:

  • 它可能在“片段”...你能显示你的 XML 吗?
  • 你能仔细检查一下帖子吗?我添加了 xml。
  • 在 Fragment 或 Activity 中执行此操作有什么关系?我看不出有什么意外……

标签: android google-maps android-fragments google-maps-android-api-2 google-maps-api-2


【解决方案1】:

当前位置..试试这个..

    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)
    {

    }

【讨论】:

  • 嘿伙计,这是在开玩笑吗?仔细查看我的帖子。在我看来,你不明白我的问题。我想知道设备的当前位置,但我发现的所有材料都使用 Activity 或 FragmentActivity 在 Fragment 中不能正常工作。所以我想知道在我的情况下如何正确地做到这一点。你有什么想法吗?
  • 您好!我看着你更新的代码,我有一些问题。您认为在使用之前检查与服务的连接更好吗?以及在哪里更好地使用您的代码?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-27
  • 2015-08-14
  • 2020-02-29
  • 2018-02-11
  • 2017-08-29
相关资源
最近更新 更多