Getting latitude and longitude from latlng object

2020-07-03 07:08发布

I am trying to get latitude and longitude on the Googlemap v2.. I have set the onclick listener for mapragment object using the code

 map.setOnMapClickListener(new OnMapClickListener() {

    @Override
    public void onMapClick(LatLng latln) {
        // TODO Auto-generated method stub


        String s=latln.toString();

        Log.w("dsfdsf",""+s);

    }
});

It provides be the latitude and longitude coordinates in the format lat/lng: (xx.xxxxxx,yy.yyyyy) I need the exact method to get latitude and longitude data.. I dont want to parse the data using split and get the coordinates..

5条回答
聊天终结者
2楼-- · 2020-07-03 07:28

For newer version please use:

double lat = latlng.lat;
double lng = latlng.lng;

as latlng.latitude and latlng.longitude or latlng.lat() and latlng.lng() will give compilation error.

查看更多
等我变得足够好
3楼-- · 2020-07-03 07:36

To get Latitude and Longitude your have to declare 2 double variables.

map.setOnMapClickListener(new OnMapClickListener() {

    @Override
    public void onMapClick(LatLng latln) {
        double latitude, longitude;
        latitude(latln.latitude);
        longitude(latln.longitude);

    }
});
查看更多
放荡不羁爱自由
4楼-- · 2020-07-03 07:42

This can easily be found in the Google Maps API reference:

public final class LatLng

public final double latitude

Latitude, in degrees. This value is in the range [-90, 90].

public final double longitude

Longitude, in degrees. This value is in the range [-180, 180).

So in your example you should call:

double lat = latlng.latitude;
double lng = latlng.longitude;
查看更多
我欲成王,谁敢阻挡
5楼-- · 2020-07-03 07:42
var res = results1[key][key1].toString().split("(");
console.log(" latlong::"+res);
var res2= res[1].split(",");
console.log(" latlong::"+res2);
latlongFrom[0]=res2[0];
console.log(" latlong::"+latlongFrom[0]);
var res3=res2[1].split(")");
console.log(" latlong::"+res3[0]);
latlongFrom[1]=res3[0];
console.log(" latlong::"+latlongFrom[1]);

This is what I have done. The res contains the value of (lat,long).

查看更多
狗以群分
6楼-- · 2020-07-03 07:43

This is for Android solution

public void onMapClick(LatLng latln) {
    String s = latln.toString();
    String[] latLng = s.substring(10, s.length() - 1).split(",");
    String sLat = latLng[0];
    String sLng = latLng[1];
    Toast.makeText(MapsActivity.this, "Latitude is: "+sLat+", Longtitude is: "+sLng, Toast.LENGTH_LONG).show();
}
查看更多
登录 后发表回答