使用片段我想在Android中获取当前位置 [英] Using fragment I want get current Location in Android

查看:146
本文介绍了使用片段我想在Android中获取当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class MapViewFragment  extends Fragment implements LocationListener

    public MapViewFragment() {
        // Required empty public constructor
    }


    public static MapViewFragment newInstance(String tabSelected) {
        MapViewFragment fragment = new MapViewFragment();
        Bundle args = new Bundle();
        args.putString(Constants.FRAG_D, tabSelected);
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MapsInitializer.initialize(getActivity());

        if (mapView != null) {
            mapView.onCreate(savedInstanceState);
        }
        initializeMap();
    }

    private void initializeMap() {
        if (googleMap == null && mapsSupported) {
            mapView = (MapView) getActivity().findViewById(R.id.map);
            googleMap = mapView.getMap();
            //setup markers etc...
        }
    }





    @Override
    public void onResume() {
        super.onResume();
        Marker m1,m2,m3;
         m1 = googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(38.609556, -1.139637))
                .anchor(0.5f, 0.5f)
                .title("Title1")
                .snippet("Snippet1"));


         m2 = googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(40.4272414,-3.7020037))
                .anchor(0.5f, 0.5f)
                .title("Title2")
                .snippet("Snippet2"));

        mapView.onResume();
        initializeMap();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

    @Override
    public void onLocationChanged(Location location) {

    }

我的代码如上所示。我可以在Android工作室中手动设置两个标记,但我想要一个标记从当前位置获取位置。
任何帮助,或任何想法?

My code is shown above. I am able to set manually two marker in Android studio, but one marker I want get location from present location. Any help, or any ideas?

推荐答案

首先尝试这个:

googleMap.setMyLocationEnabled(true);

这将显示页面右上角的箭头,如果有的话,它会让你看到你的位置已启用。
此按钮还将触发google api的 onMyLocationButtonClick()功能。

This will show the arrow on the right top of the page which will make you see your location if it is enabled. This button will also trigger onMyLocationButtonClick() function of google api.

然后获取当前位置信息:

Then get the current location info:

    Criteria criteria = new Criteria();
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    String provider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(provider);
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    myPosition = new LatLng(latitude, longitude);

最后在其上添加您的标记:

Finally add your marker on it:

googleMap.addMarker(new MarkerOptions().position(myPosition).title("Marker"));

有关例外情况和更多信息: https://developers.google.com/maps/documentation/android-api/location

For exceptions and further information: https://developers.google.com/maps/documentation/android-api/location

这篇关于使用片段我想在Android中获取当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆