How can I get latitude from a city name like Florida how can I get the latitude and longitude from sending a JSON request to a Google map API.
Is there any way to do this?
I need a solution in JavaScript or jQuery throu开发者_如何转开发gh JSON.
Not sure what you mean when you say send json but you can do it with this function:
function getLatLong(address){
var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
return results[0].geometry.location;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
that will return an object which you can reference with .lat() and .lng() to get the latitude and longitude.
You can use :
var address = "New Delhi";
$.ajax({
url: "http://maps.googleapis.com/maps/api/geocode/json?address="+address+"&sensor=false",
type: "POST",
success: function(res){
console.log(res.results[0].geometry.location.lat);
console.log(res.results[0].geometry.location.lng);
}
});
Sample Response:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Address",
"short_name" : "Address",
"types" : [ "point_of_interest", "establishment" ]
},
{
"long_name" : "Madrid",
"short_name" : "Madrid",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Madrid",
"short_name" : "M",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Community of Madrid",
"short_name" : "Community of Madrid",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Spain",
"short_name" : "ES",
"types" : [ "country", "political" ]
},
{
"long_name" : "28039",
"short_name" : "28039",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Address, 28039 Madrid, Madrid, Spain",
"geometry" : {
"location" : {
"lat" : 40.447337,
"lng" : -3.7070179
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.44868598029151,
"lng" : -3.705668919708498
},
"southwest" : {
"lat" : 40.44598801970851,
"lng" : -3.708366880291502
}
}
},
"place_id" : "ChIJAcX0u1YoQg0R6ZlFdkOQgIA",
"types" : [ "hospital", "point_of_interest", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "ADDRESS",
"short_name" : "ADDRESS",
"types" : [ "premise" ]
},
{
"long_name" : "19",
"short_name" : "19",
"types" : [ "sublocality_level_4", "sublocality", "political" ]
},
{
"long_name" : "7",
"short_name" : "7",
"types" : [ "sublocality_level_3", "sublocality", "political" ]
},
{
"long_name" : "2 Chome",
"short_name" : "2 Chome",
"types" : [ "sublocality_level_2", "sublocality", "political" ]
},
{
"long_name" : "Nakaimaizumi",
"short_name" : "Nakaimaizumi",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Utsunomiya-shi",
"short_name" : "Utsunomiya-shi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Tochigi-ken",
"short_name" : "Tochigi-ken",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Japan",
"short_name" : "JP",
"types" : [ "country", "political" ]
},
{
"long_name" : "321-0968",
"short_name" : "321-0968",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Japan, 〒321-0968 Tochigi-ken, Utsunomiya-shi, Nakaimaizumi, 2 Chome−7−19 ADDRESS",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 36.5678955,
"lng" : 139.9034399
},
"southwest" : {
"lat" : 36.5675808,
"lng" : 139.9032638
}
},
"location" : {
"lat" : 36.5677206,
"lng" : 139.9033588
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 36.5690871302915,
"lng" : 139.9047008302915
},
"southwest" : {
"lat" : 36.5663891697085,
"lng" : 139.9020028697085
}
}
},
"place_id" : "ChIJ4zq2MgxnH2ARrK-H9cHhAjE",
"types" : [ "premise" ]
},
{
"address_components" : [
{
"long_name" : "Address:",
"short_name" : "Address:",
"types" : [ "point_of_interest", "establishment" ]
},
{
"long_name" : "Pineville",
"short_name" : "Pineville",
"types" : [ "locality", "political" ]
},
{
"long_name" : "1, Charlotte",
"short_name" : "1, Charlotte",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Mecklenburg County",
"short_name" : "Mecklenburg County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "North Carolina",
"short_name" : "NC",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "28134",
"short_name" : "28134",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Address:, Pineville, NC 28134, USA",
"geometry" : {
"location" : {
"lat" : 35.04203100000001,
"lng" : -80.863066
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.04337998029151,
"lng" : -80.86171701970851
},
"southwest" : {
"lat" : 35.04068201970851,
"lng" : -80.86441498029151
}
}
},
"place_id" : "ChIJ4dWtti2DVogRigDBRFUTccg",
"types" : [ "point_of_interest", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "Address",
"short_name" : "Address",
"types" : [ "premise" ]
},
{
"long_name" : "AGB Colony Kacha Bazar Road",
"short_name" : "AGB Colony Kacha Bazar Rd",
"types" : [ "route" ]
},
{
"long_name" : "AGB Colony",
"short_name" : "AGB Colony",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Motijheel",
"short_name" : "Motijheel",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Dhaka",
"short_name" : "Dhaka",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Dhaka",
"short_name" : "Dhaka",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Dhaka Division",
"short_name" : "Dhaka Division",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Bangladesh",
"short_name" : "BD",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Address, AGB Colony Kacha Bazar Road, Dhaka, Bangladesh",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 23.7329981,
"lng" : 90.4223152
},
"southwest" : {
"lat" : 23.7326888,
"lng" : 90.4219839
}
},
"location" : {
"lat" : 23.7328435,
"lng" : 90.4221496
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 23.7341924302915,
"lng" : 90.42349853029151
},
"southwest" : {
"lat" : 23.73149446970849,
"lng" : 90.42080056970849
}
}
},
"place_id" : "ChIJ4RhdqkS4VTcR9QELu9rNaAQ",
"types" : [ "premise" ]
},
{
"address_components" : [
{
"long_name" : "Address",
"short_name" : "Address",
"types" : [ "premise" ]
},
{
"long_name" : "3",
"short_name" : "3",
"types" : [ "street_number" ]
},
{
"long_name" : "Sweet Fern Lane",
"short_name" : "Sweet Fern Ln",
"types" : [ "route" ]
},
{
"long_name" : "Coventry",
"short_name" : "Coventry",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Kent County",
"short_name" : "Kent County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Rhode Island",
"short_name" : "RI",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "02816",
"short_name" : "02816",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Address, 3 Sweet Fern Lane, Coventry, RI 02816, USA",
"geometry" : {
"location" : {
"lat" : 41.6763577,
"lng" : -71.53721809999999
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 41.6777066802915,
"lng" : -71.53586911970849
},
"southwest" : {
"lat" : 41.6750087197085,
"lng" : -71.5385670802915
}
}
},
"place_id" : "ChIJ9cdx-89K5IkR24VLhLim8is",
"types" : [ "premise" ]
},
{
"address_components" : [
{
"long_name" : "Address",
"short_name" : "Address",
"types" : [ "point_of_interest", "establishment" ]
},
{
"long_name" : "Chandolli Talli",
"short_name" : "Chandolli Talli",
"types" : [ "street_number" ]
},
{
"long_name" : "Nayapanchayat Chetra Dumaila { Bironkhal }",
"short_name" : "Nayapanchayat Chetra Dumaila { Bironkhal }",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Patti Khatli",
"short_name" : "Patti Khatli",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Chandolli Talli",
"short_name" : "Chandolli Talli",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Pauri Garhwal",
"short_name" : "Pauri Garhwal",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Uttarakhand",
"short_name" : "UK",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
},
{
"long_name" : "246177",
"short_name" : "246177",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Address, Chandolli Talli, Nayapanchayat Chetra Dumaila { Bironkhal }, Patti Khatli, Chandolli Talli, Uttarakhand 246177, India",
"geometry" : {
"location" : {
"lat" : 29.839023,
"lng" : 78.9964554
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 29.8403719802915,
"lng" : 78.99780438029151
},
"southwest" : {
"lat" : 29.8376740197085,
"lng" : 78.99510641970849
}
}
},
"place_id" : "ChIJH609gR-NCTkR6XqpwaxHEJU",
"types" : [ "place_of_worship", "point_of_interest", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "The Address",
"short_name" : "The Address",
"types" : [ "route" ]
},
{
"long_name" : "Hope Island",
"short_name" : "Hope Island",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Queensland",
"short_name" : "QLD",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Australia",
"short_name" : "AU",
"types" : [ "country", "political" ]
},
{
"long_name" : "4212",
"short_name" : "4212",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "The Address, Hope Island QLD 4212, Australia",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : -27.8585983,
"lng" : 153.3562717
},
"southwest" : {
"lat" : -27.8603566,
"lng" : 153.3527792
}
},
"location" : {
"lat" : -27.8599467,
"lng" : 153.3547341
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : -27.8581284697085,
"lng" : 153.3562717
},
"southwest" : {
"lat" : -27.8608264302915,
"lng" : 153.3527792
}
}
},
"partial_match" : true,
"place_id" : "ChIJIZJj8TcSkWsROGKFb_QL2bs",
"types" : [ "route" ]
},
{
"address_components" : [
{
"long_name" : "address",
"short_name" : "address",
"types" : [ "premise" ]
},
{
"long_name" : "7",
"short_name" : "7",
"types" : [ "sublocality_level_3", "sublocality", "political" ]
},
{
"long_name" : "Shōdokorochō",
"short_name" : "Shōdokorochō",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Takatsuki-shi",
"short_name" : "Takatsuki-shi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Ōsaka-fu",
"short_name" : "Ōsaka-fu",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Japan",
"short_name" : "JP",
"types" : [ "country", "political" ]
},
{
"long_name" : "569-0064",
"short_name" : "569-0064",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Japan, 〒569-0064 Ōsaka-fu, Takatsuki-shi, Shōdokorochō, 7 address",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 34.8398552,
"lng" : 135.6145781
},
"southwest" : {
"lat" : 34.8398113,
"lng" : 135.6145126
}
},
"location" : {
"lat" : 34.8398268,
"lng" : 135.6145553
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 34.84118223029149,
"lng" : 135.6158943302915
},
"southwest" : {
"lat" : 34.8384842697085,
"lng" : 135.6131963697085
}
}
},
"place_id" : "ChIJkQ3tc0wdAWARodHx1Sz3pI4",
"types" : [ "premise" ]
},
{
"address_components" : [
{
"long_name" : "Address",
"short_name" : "Address",
"types" : []
},
{
"long_name" : "28",
"short_name" : "28",
"types" : [ "street_number" ]
},
{
"long_name" : "Tanza - Trece Martires Road",
"short_name" : "Tanza - Trece Martires Rd",
"types" : [ "route" ]
},
{
"long_name" : "Trece Martires City",
"short_name" : "Trece Martires City",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Cavite",
"short_name" : "Cavite",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Calabarzon",
"short_name" : "Calabarzon",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Philippines",
"short_name" : "PH",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Address, 28 Tanza - Trece Martires Road, Trece Martires City, Cavite, Philippines",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 14.2919207,
"lng" : 120.8680844
},
"southwest" : {
"lat" : 14.2915517,
"lng" : 120.8676444
}
},
"location" : {
"lat" : 14.2917362,
"lng" : 120.8678644
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 14.2930851802915,
"lng" : 120.8692133802915
},
"southwest" : {
"lat" : 14.2903872197085,
"lng" : 120.8665154197085
}
}
},
"place_id" : "ChIJYUlfGnSAvTMRc34heYWlFw0",
"types" : []
},
{
"address_components" : [
{
"long_name" : "ADDRESS",
"short_name" : "ADDRESS",
"types" : [ "premise" ]
},
{
"long_name" : "4",
"short_name" : "4",
"types" : [ "sublocality_level_3", "sublocality", "political" ]
},
{
"long_name" : "2 Chome",
"short_name" : "2 Chome",
"types" : [ "sublocality_level_2", "sublocality", "political" ]
},
{
"long_name" : "Minamimachi",
"short_name" : "Minamimachi",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Mito-shi",
"short_name" : "Mito-shi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Ibaraki-ken",
"short_name" : "Ibaraki-ken",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Japan",
"short_name" : "JP",
"types" : [ "country", "political" ]
},
{
"long_name" : "310-0021",
"short_name" : "310-0021",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Japan, 〒310-0021 Ibaraki-ken, Mito-shi, Minamimachi, 2 Chome−4 ADDRESS",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 36.3752077,
"lng" : 140.471904
},
"southwest" : {
"lat" : 36.3751499,
"lng" : 140.4718129
}
},
"location" : {
"lat" : 36.3751785,
"lng" : 140.4718554
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 36.3765277802915,
"lng" : 140.4732074302915
},
"southwest" : {
"lat" : 36.3738298197085,
"lng" : 140.4705094697085
}
}
},
"place_id" : "ChIJjdzYU1MkImAR2HGAxMHMgPA",
"types" : [ "premise" ]
},
{
"address_components" : [
{
"long_name" : "ADDRESS",
"short_name" : "ADDRESS",
"types" : [ "premise" ]
},
{
"long_name" : "12",
"short_name" : "12",
"types" : [ "sublocality_level_3", "sublocality", "political" ]
},
{
"long_name" : "5 Chome",
"short_name" : "5 Chome",
"types" : [ "sublocality_level_2", "sublocality", "political" ]
},
{
"long_name" : "Kokuryōchō",
"short_name" : "Kokuryōchō",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Chōfu-shi",
"short_name" : "Chōfu-shi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Tōkyō-to",
"short_name" : "Tōkyō-to",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Japan",
"short_name" : "JP",
"types" : [ "country", "political" ]
},
{
"long_name" : "182-0022",
"short_name" : "182-0022",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Japan, 〒182-0022 Tōkyō-to, Chōfu-shi, Kokuryōchō, 5 Chome−12 ADDRESS",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.6490935,
"lng" : 139.5517727
},
"southwest" : {
"lat" : 35.6489615,
"lng" : 139.5515341
}
},
"location" : {
"lat" : 35.6490117,
"lng" : 139.5516496
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 35.6503764802915,
"lng" : 139.5530023802915
},
"southwest" : {
"lat" : 35.6476785197085,
"lng" : 139.5503044197085
}
}
},
"place_id" : "ChIJcxR2uEPwGGAR42ysbaayK_0",
"types" : [ "premise" ]
}
],
"status" : "OK"
}
Demo : http://jsbin.com/riyakobero/edit?html,js,console,output
精彩评论