java - 谷歌地图不返回经度和纬度

标签 java android google-maps android-fragments google-maps-api-3

当我试图在谷歌地图上获取我当前位置的纬度和经度时,它没有返回任何值,也没有使我的按钮能够获取当前位置。

我是 android 的新手。

public class ChatFragment extends Fragment{
    // Google Map
    private GoogleMap googleMap;
    GPSTracker gpsTracker = new GPSTracker(getContext());
    protected String latitude,longitude;

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View chatfrag = inflater.inflate(R.layout.chatfragment, container, false);


        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return chatfrag;
    }

    /**
     * function to load map. If map is not created it will create it for you
     */
    private void initilizeMap()
    {
        if (googleMap == null) {
            SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

            googleMap = mapFrag.getMap();
            LocationManager   locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) this);
            Location getLastLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
            double currentlong = getLastLocation.getLatitude();

            double currentlat = getLastLocation.getLongitude();

            System.out.print(currentlat);
            System.out.print(currentlong);

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

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getActivity(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
//      initilizeMap();
    }
}

如有任何帮助,我们将不胜感激。

最佳答案

如果你想从谷歌地图获取当前位置然后试试这个,

googleMap.setMyLocationEnabled(true);
double currentlong = googleMap.getMyLocation().getLatitude();
double currentlat = googleMap.getMyLocation().getLongitude();

// if you want to get location update then implement this
googleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
    @Override
    public void onMyLocationChange(Location loc) {
        // get current location update
    }
});

希望对你有用。

关于java - 谷歌地图不返回经度和纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36445050/

相关文章:

java - 使用 Java Streams 获取嵌套在 HashMap 中的元素最多的集合

javascript - Bootstrap Modal 上的 Google map 为灰色 - 调整窗口大小使其显示

java - 如何从 hibernate 中的实体类的结果中获取数据

android - ConstraintLayout中如何用两个view填满屏幕?

android - 谷歌地图 MapFragment 导致应用程序崩溃

java - Android:Canvas Arc,Sweep Gradient Start Angle可以改吗?

javascript - 如何在 google.maps.event.addListener 中使用它

android - 从 JSON 响应在 Google map 上添加多个标记

java - jsp 的 Web 容器

Java Swing - 如何使 JButton 的边界不可见?