I have the following code which through Google Maps API gets the latitude and longitude of the position clicked on the map
var latitude_longitude = evt.latLng;
When I alert the variable using the code below the following is displayed: (53.4128,-1.51165)
alert(latitude_longitude);////Gets latitude and Longitude - Latitude first - Longitude second
However I now need to split (53.4128,-1.51165) into 2 variables Latitude and Longitude and have tried using the code below:
var myarr = latitude_longitude.split(",");
var latitude = myarr[0];
alert(latitude);
However the alert doesn't show and I am unsure why?
你可以试试:
var latitude = evt.latLng.lat();
Given this:
var latlng = '53.4128,-1.51165';
console.log(latlng);
var lat = latlng.split(",")[0];
console.log(lat);
I get the output:
53.4128,-1.51165
53.4128
So splitting on the comma is perfectly valid for a string variable with a comma in it. However, I suspect that your latitude_longitude
is not a string. Open Developer Tools (or its equivalent for the browser you are using), set a breakpoint and examine the variable. I suspect you will see that it is an object with properties and that you can just access those properties instead of trying to parse its string representation.
first check if your variable is an object or string:
alert(typeof latitude_longitude);
if your alert output is "object" so you need to convert it to string
var strLatLong=String(latitude_longitude);
var LangLat = strLatLong.split(",");
alert(typeof strLatLong);
....it worked for me
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.