问题
I wanna get the block number of an address located in Bahrain.
first of all, let me show you what is block numbers. Bahrain is divided into areas and each area has its block number like in the photo.
so, I hope the block number becomes clear to you now.
I've implemented a basic app to get the location and some of its info like this
this address is located in BLOCK 326 but I don't have any idea about how getting the correct block num.
this feature is already built-in Uber and trycarriage apps.
I'm using Google's API to get my data.
so, any good ideas about how could that be done?
回答1:
You can use Reverse geocoding request of Geocoding API:
https://maps.googleapis.com/maps/api/geocode/json?latlng=<LATITUDE>, <LONGITURE>&key=<YOUR_API_KEY>
and parse its JSON result. For example for
https://maps.googleapis.com/maps/api/geocode/json?latlng=26.218588, 50.587587&key=<YOUR_API_KEY>
you got something like that:
{
"plus_code" : {
"compound_code" : "6H9Q+C2 Manama, Bahrain",
"global_code" : "7HRG6H9Q+C2"
},
"results" : [
{
"address_components" : [
{
"long_name" : "Bahrain",
"short_name" : "BH",
"types" : [ "country", "political" ]
},
{
"long_name" : "القضيبية",
"short_name" : "القضيبية",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "المنامة",
"short_name" : "المنامة",
"types" : [ "locality", "political" ]
},
{
"long_name" : "محافظة العاصمة",
"short_name" : "محافظة العاصمة",
"types" : [ "administrative_area_level_1", "political" ]
}
],
"formatted_address" : "Road 596, Build 326 , Flat 12 - Adliya , Bahrain, المنامة، Bahrain",
"geometry" : {
"location" : {
"lat" : 26.218734,
"lng" : 50.587591
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 26.2200829802915,
"lng" : 50.5889399802915
},
"southwest" : {
"lat" : 26.21738501970849,
"lng" : 50.5862420197085
}
}
},
"place_id" : "ChIJf7pq3DevST4RM9wS0YnAkGk",
"plus_code" : {
"compound_code" : "6H9Q+F2 Manama, Bahrain",
"global_code" : "7HRG6H9Q+F2"
},
"types" : [ "establishment", "point_of_interest" ]
},
{
"address_components" : [
{
"long_name" : "Hassan Bin Thabit Avenue",
"short_name" : "Hassan Bin Thabit Ave",
"types" : [ "route" ]
},
{
"long_name" : "Qudaibiya",
"short_name" : "Qudaibiya",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Manama",
"short_name" : "Manama",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Capital Governorate",
"short_name" : "Capital Governorate",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Bahrain",
"short_name" : "BH",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Hassan Bin Thabit Ave, Manama, Bahrain",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 26.218572,
"lng" : 50.58771420000001
},
"southwest" : {
"lat" : 26.2184482,
"lng" : 50.5874591
}
},
"location" : {
"lat" : 26.2185067,
"lng" : 50.5875887
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 26.2198590802915,
"lng" : 50.5889356302915
},
"southwest" : {
"lat" : 26.2171611197085,
"lng" : 50.5862376697085
}
}
},
"place_id" : "ChIJyekp3TevST4Rfj8TaosVDpE",
"types" : [ "route" ]
},
{
"address_components" : [
{
"long_name" : "Block 326",
"short_name" : "Block 326",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Qudaibiya",
"short_name" : "Qudaibiya",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Manama",
"short_name" : "Manama",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Capital Governorate",
"short_name" : "Capital Governorate",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Bahrain",
"short_name" : "BH",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Block 326, Manama, Bahrain",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 26.2234075,
"lng" : 50.5912257
},
"southwest" : {
"lat" : 26.2153032,
"lng" : 50.5850888
}
},
"location" : {
"lat" : 26.2198723,
"lng" : 50.5879925
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 26.2234075,
"lng" : 50.5912257
},
"southwest" : {
"lat" : 26.2153032,
"lng" : 50.5850888
}
}
},
"place_id" : "ChIJb9O71TevST4RqB02rRcRh1g",
"types" : [ "neighborhood", "political" ]
},
{
"address_components" : [
{
"long_name" : "Qudaibiya",
"short_name" : "Qudaibiya",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "Manama",
"short_name" : "Manama",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Capital Governorate",
"short_name" : "Capital Governorate",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Bahrain",
"short_name" : "BH",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Qudaibiya, Manama, Bahrain",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 26.2297388,
"lng" : 50.59774059999999
},
"southwest" : {
"lat" : 26.2077109,
"lng" : 50.5822785
}
},
"location" : {
"lat" : 26.2186782,
"lng" : 50.5938215
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 26.2297388,
"lng" : 50.59774059999999
},
"southwest" : {
"lat" : 26.2077109,
"lng" : 50.5822785
}
}
},
"place_id" : "ChIJic3RMUivST4Ru9BNKfSYXcw",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"address_components" : [
{
"long_name" : "Manama",
"short_name" : "Manama",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Capital Governorate",
"short_name" : "Capital Governorate",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Bahrain",
"short_name" : "BH",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Manama, Bahrain",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 26.262323,
"lng" : 50.62693119999999
},
"southwest" : {
"lat" : 26.1909103,
"lng" : 50.5511212
}
},
"location" : {
"lat" : 26.2235305,
"lng" : 50.5875935
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 26.262323,
"lng" : 50.62693119999999
},
"southwest" : {
"lat" : 26.1909103,
"lng" : 50.5511212
}
}
},
"place_id" : "ChIJLaZ2J3KvST4RwmAPB6Y4Z4s",
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Capital Governorate",
"short_name" : "Capital Governorate",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Bahrain",
"short_name" : "BH",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Capital Governorate, Bahrain",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 26.262323,
"lng" : 50.62693119999999
},
"southwest" : {
"lat" : 26.1909103,
"lng" : 50.51370679999999
}
},
"location" : {
"lat" : 26.2285161,
"lng" : 50.5860497
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 26.262323,
"lng" : 50.62693119999999
},
"southwest" : {
"lat" : 26.1909103,
"lng" : 50.51370679999999
}
}
},
"place_id" : "ChIJo5Ng-m-vST4RGbXeFQV4px4",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "Bahrain",
"short_name" : "BH",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Bahrain",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 26.3469001,
"lng" : 50.8509064
},
"southwest" : {
"lat" : 25.5349999,
"lng" : 50.324246
}
},
"location" : {
"lat" : 26.0667,
"lng" : 50.5577
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 26.3469001,
"lng" : 50.8509064
},
"southwest" : {
"lat" : 25.5349999,
"lng" : 50.324246
}
}
},
"place_id" : "ChIJEaJHak5SSD4RRhDa2-JQlC4",
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
where you can find block name e.g. at "formatted_address" tag:
"formatted_address" : "Block 326, Manama, Bahrain",
So, you just need to carefully parse JSON response.
来源:https://stackoverflow.com/questions/59349438/extract-block-number-from-address-located-in-bahrain