使用Google地图v2获取经度和纬度

时间:2014-06-02 04:53:59

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

如何使用Google maps v2获取当前手机位置的经度?

我使用此方法缩放设备位置:

googleMap.getUiSettings().setMyLocationButtonEnabled(true);

现在我如何获得坐标?

我知道这种获取区域的方法:

        double left = vr.latLngBounds.southwest.longitude;
        double top = vr.latLngBounds.northeast.latitude; ...

3 个答案:

答案 0 :(得分:4)

您可以通过此代码从谷歌地图获取中心的Lat和Lng值

LatLng latLng = map.getCameraPosition().target;
double lat = latLng.latitude;
double lng = latLng.longitude;

答案 1 :(得分:1)

Google Maps API位置包含侦听器,例如:

private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new 

GoogleMap.OnMyLocationChangeListener() {
@Override

public void onMyLocationChange(Location location) {
    LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
    mMarker = gMap.addMarker(new MarkerOptions().position(loc));
    if(gMap != null){
        gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
    }
}

};

然后设置地图的监听器:

gMap.setOnMyLocationChangeListener(myLocationChangeListener);

当地图首次找到该位置时,将调用此方法。 试试这段代码。

答案 2 :(得分:0)

使用此:

private boolean gps_enabled = false;
private boolean network_enabled = false;
private Location location;
private void getMyCurrentLocation() {
    Double MyLat = null, MyLong = null;
    String CityName = "";
    String StateName = "";
    String CountryName = "";
    LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener locListener = new MyLocationListener();

    try {
        gps_enabled = locManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
    }
    try {
        network_enabled = locManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
    }

    // don't start listeners if no provider is enabled
    // if(!gps_enabled && !network_enabled)
    // return false;

    if (gps_enabled) {
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, locListener);

    }

    if (gps_enabled) {
        location = locManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);

    }

    if (network_enabled && location == null) {
        locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                0, 0, locListener);

    }

    if (network_enabled && location == null) {
        location = locManager
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    }

    if (location != null) {

        MyLat = location.getLatitude();
        MyLong = location.getLongitude();
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                MyLat, MyLong), 15));

    } else {
        Location loc = getLastKnownLocation(this);
        if (loc != null) {

            MyLat = loc.getLatitude();
            MyLong = loc.getLongitude();

        }
    }
    locManager.removeUpdates(locListener); // removes the periodic updates




}

public class MyLocationListener implements LocationListener {
    public void onLocationChanged(Location location) {
        if (location != null) {
        }
    }

    public void onProviderDisabled(String provider) {
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}

public static Location getLastKnownLocation(Context context) {
    Location location = null;
    LocationManager locationmanager = (LocationManager) context
            .getSystemService("location");
    List list = locationmanager.getAllProviders();
    boolean i = false;
    Iterator iterator = list.iterator();
    do {
        if (!iterator.hasNext())
            break;
        String s = (String) iterator.next();
        if (i != false && !locationmanager.isProviderEnabled(s))
            continue;
        Location location1 = locationmanager.getLastKnownLocation(s);
        if (location1 == null)
            continue;
        if (location != null) {
            float f = location.getAccuracy();
            float f1 = location1.getAccuracy();
            if (f >= f1) {
                long l = location1.getTime();
                long l1 = location.getTime();
                if (l - l1 <= 600000L)
                    continue;
            }
        }
        location = location1;
        i = locationmanager.isProviderEnabled(s);
    } while (true);
    return location;
}