I'm using Google Places via:
https://ubilabs.github.io/geocomplete/
var geocomp = $("#geocomplete").geocomplete({types:['establishment', 'geocode']});
I get back the following JSON:
{
"address_components": [
{
"long_name": "Empire State Building",
"short_name": "Empire State Building",
"types": [
"point_of_interest",
"establishment"
]
},
{
},
…{
}
],
"formatted_address": "Empire State Building, 350 5th Ave, New York, NY 10118, USA",
"geometry": {
"location": {
"lat": 40.7484405,
"lng": -73.98566440000002
},
"location_type": "APPROXIMATE",
"viewport": {
"south": 40.7470915197085,
"west": -73.9870133802915,
"north": 40.7497894802915,
"east": -73.98431541970848
}
},
"place_id": "ChIJaXQRs6lZwokRY6EFpJnhNNE",
"types": [
"point_of_interest",
"establishment"
]
}
It returns the JSON data in the following callback:
$("#geocomplete")
.bind("geocode:result", function(event, result){
}
I'm able to access elements of the address_components array as follows:
result.address_components[0].long_name
result.address_components[1].long_name
result.address_components[2].long_name
How do I access "lat" and "lng" in location.geometry?
I've tried
result.geometry.location.lat
result.geometry.location.lng
and says lat contains:
function (){return a}
and lng contains:
function (){return b}
which makes no sense to me give the JSON included at the beginning.
How do i access those 2 values?
I think location
is a google.maps.LatLng
object. Anyway, you can access the coordinates using result.geometry.location.lat()
and result.geometry.location.lng()
.
Here is what you need: My example shows how to get data of a specific address but I think it will work.
Classes:
public class GoogleGeoCodeResponse
{
public string status { get; set; }
public results[] results { get; set; }
public string error_message { get; set; }
public string next_page_token { get; set; }
}
public class results
{
public string name { get; set; }
public string formatted_address { get; set; }
public geometry geometry { get; set; }
public string[] types { get; set; }
public address_component[] address_components { get; set; }
public string vicinity { get; set; }
public string place_id { get; set; }
public string international_phone_number { get; set; }
}
public class geometry
{
public string location_type { get; set; }
public location location { get; set; }
}
public class location
{
public string lat { get; set; }
public string lng { get; set; }
}
public class GooglePalceDetail
{
public results result { get; set; }
public string status { get; set; }
public string error_message { get; set; }
}
public class address_component
{
public string long_name { get; set; }
public string short_name { get; set; }
public string[] types { get; set; }
}
Controller:
var address = string.Format("https://maps.googleapis.com/maps/api/geocode/json?address={0}&key={1}", currentAddress, googleKey);
string json = client.DownloadString(address);
var gglGeo = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(json);
if ((gglGeo.status == "ZERO_RESULTS")){
ViewBag.msg="no results";
return View()
}
var lat = gglGeo.results[0].geometry.location.lat;
var lng = gglGeo.results[0].geometry.location.lng;
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.