简体   繁体   English

我的应用程序中的 GPS 位置

[英]GPS location in my app

Im having trouble passing the current location to my LatLng variable to show on my google map.我无法将当前位置传递给我的 LatLng 变量以显示在我的谷歌地图上。 Im not sure how to pass the value from my override statement in my location listener method.我不确定如何在我的位置侦听器方法中从我的覆盖语句传递值。 Im trying to implement an application that simply gets the GPS coordinates and places a marker on the map.我试图实现一个应用程序,它只是获取 GPS 坐标并在地图上放置一个标记。

    @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_maps);


    locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);

    //obtain location
    listener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            LatLng appoint = new LatLng(lat,lng);
            return appoint;
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {
        }

        @Override
        public void onProviderDisabled(String s) {
        }
    };

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    double lat = 34.014783;
    double lng = -84.571381;

    // Add a marker in Appoint from database and move the camera
    LatLng appoint = new LatLng(lat,lng);
    mMap.addMarker(new MarkerOptions().position(appoint).title("Where the wild gays are"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(appoint));`

Well, onLocationChanged doesn't return anything void so you cannot return the LatLng object, but you can add the marker to the mMap object (as long as it is not null).好吧, onLocationChanged不返回任何void因此您无法返回LatLng对象,但您可以将marker添加到mMap对象(只要它不为空)。

Eg例如

public void onLocationChanged(Location location) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        LatLng appoint = new LatLng(lat,lng);
        if(mMap != null) {
            mMap.addMarker(new MarkerOptions().position(appoint).title("Where the wild gays are"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(appoint));
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM