获取cn1中的位置名称(反向地理编码)

时间:2018-01-05 14:38:03

标签: codenameone

我使用getLocationManager作为以下代码在地图中显示了位置(纬度和经度)。我怎样才能获得位置名称(反向地理编码?

final MapContainer cnt = new MapContainer();
cnt.setCameraPosition(new Coord(lat, lng));
cnt.addMarker(
        EncodedImage.createFromImage(markerImg, false),
        new Coord(lat, lng),
        "marker",
        "Optional long description",
        evt -> {

        }
);
cnt.zoom(new Coord(lat, lng), 15);
Container mapParent = new Container(new BorderLayout());

Label mapPosition = new Label("");
mapPosition .setText(lat + ", " + lng);//name of the place instead of lat, long here
if (lat == 0) {
    mapPosition .setText("Location n/a. Please go outside and check in again");
}

GPS值

public final void checkGPS() {
    if (Display.getInstance().getLocationManager().isGPSDetectionSupported()) {

        LocationManager.getLocationManager().setLocationListener(new LocationListener() {

            @Override
            public void locationUpdated(Location location) {
                loc = location;
                lat = location.getLatitude();
                lng = location.getLongitude();
            }

            @Override
            public void providerStateChanged(int newState) {
            }
        });
    } else {

    }
}

更新

public static void nameMyCurrentLocation(Location l) {
    if (l == null) {
        return;
    }
    if (lastLocationRequest != null) {
        lastLocationRequest.kill();
    }
    lastLocationRequest = Rest.get("https://maps.googleapis.com/maps/api/geocode/json").
            queryParam("latlng", 27.700769 + "," + 85.300140).
            queryParam("key", "AIzaSyCnWurwIKdfqXLsrWakuI2Boc9kdiQ9xhI").
            queryParam("language", "en").
            queryParam("result_type", "street_address|point_of_interest").
            getAsJsonMap(callbackMap -> {
                Map data = callbackMap.getResponseData();
                System.out.println("xxx " + data);
                if (data != null) {
                    ArrayList results = (ArrayList) data.get("results");
                    if (results != null && results.size() > 0) {
                        Map firstResult = (Map) results.get(0);
                        name.onSucess((String) firstResult.get("formatted_address"));
                   //   System.out.println("geo location " + firstResult);
                    }
                }
            });
}

输出:

xxx {results = [],status = ZERO_RESULTS}

从文档中: “ZERO_RESULTS”表示地理编码成功但未返回任何结果。如果地理编码器传递了一个不存在的地址,则可能会发生这种情况。

lat长期存在的任何东西,我也尝试了其他东西,但都给出了zero_results状态错误。那是为什么?

删除此行后的Json输出“queryParam(”result_type“,”street_address | point_of_interest“)”

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Wyawasayi Galli",
               "short_name" : "Wyawasayi Galli",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Kalimati",
               "short_name" : "Kalimati",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Kathmandu",
               "short_name" : "Kathmandu",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Bagmati",
               "short_name" : "Bagmati",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Central Development Region",
               "short_name" : "Central Development Region",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Nepal",
               "short_name" : "NP",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "44600",
               "short_name" : "44600",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Wyawasayi Galli, Kathmandu 44600, Nepal",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 27.7015015,
                  "lng" : 85.299655
               },
               "southwest" : {
                  "lat" : 27.7007815,
                  "lng" : 85.2991827
               }
            },
            "location" : {
               "lat" : 27.7011091,
               "lng" : 85.29936099999999
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 27.7024904802915,
                  "lng" : 85.30076783029152
               },
               "southwest" : {
                  "lat" : 27.6997925197085,
                  "lng" : 85.2980698697085
               }
            }
         },
         "place_id" : "ChIJCTorGFkY6zkRqRH2VjYvqg8",
         "types" : [ "route" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Tankeshwor",
               "short_name" : "Tankeshwor",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Kalimati",
               "short_name" : "Kalimati",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Kathmandu",
               "short_name" : "Kathmandu",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Kathmandu",
               "short_name" : "Kathmandu",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Bagmati",
               "short_name" : "Bagmati",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Central Development Region",
               "short_name" : "Central Development Region",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Nepal",
               "short_name" : "NP",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "44600",
               "short_name" : "44600",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Tankeshwor, Kathmandu 44600, Nepal",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 27.7013451,
                  "lng" : 85.30279759999999
               },
               "southwest" : {
                  "lat" : 27.6981819,
                  "lng" : 85.2992313
               }
            },
            "location" : {
               "lat" : 27.6993036,
               "lng" : 85.30166509999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 27.7013451,
                  "lng" : 85.30279759999999
               },
               "southwest" : {
                  "lat" : 27.6981819,
                  "lng" : 85.2992313
               }
            }
         },
         "place_id" : "ChIJjeLvm1kY6zkRA1w61Yedm_E",
         "types" : [ "neighborhood", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Kalimati",
               "short_name" : "Kalimati",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Kathmandu",
               "short_name" : "Kathmandu",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Bagmati",
               "short_name" : "Bagmati",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Central Development Region",
               "short_name" : "Central Development Region",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Nepal",
               "short_name" : "NP",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "44600",
               "short_name" : "44600",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Kalimati, Kathmandu 44600, Nepal",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 27.7047408,
                  "lng" : 85.3029018
               },
               "southwest" : {
                  "lat" : 27.6933613,
                  "lng" : 85.2812261
               }
            },
            "location" : {
               "lat" : 27.6999738,
               "lng" : 85.2890594
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 27.7047408,
                  "lng" : 85.3029018
               },
               "southwest" : {
                  "lat" : 27.6933613,
                  "lng" : 85.2812261
               }
            }
         },
         "place_id" : "ChIJhdMirmAY6zkRUu9MKOhEJHw",
         "types" : [ "political", "sublocality", "sublocality_level_1" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Kathmandu",
               "short_name" : "Kathmandu",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Bagmati",
               "short_name" : "Bagmati",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Central Development Region",
               "short_name" : "Central Development Region",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Nepal",
               "short_name" : "NP",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "44600",
               "short_name" : "44600",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Kathmandu 44600, Nepal",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 27.7499367,
                  "lng" : 85.37316799999999
               },
               "southwest" : {
                  "lat" : 27.667984,
                  "lng" : 85.2790976
               }
            },
            "location" : {
               "lat" : 27.7172453,
               "lng" : 85.3239605
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 27.7499367,
                  "lng" : 85.37316799999999
               },
               "southwest" : {
                  "lat" : 27.667984,
                  "lng" : 85.2790976
               }
            }
         },
         "place_id" : "ChIJv6p7MIoZ6zkR6rGN8Rt8E7U",
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Kathmandu",
               "short_name" : "Kathmandu",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Bagmati",
               "short_name" : "Bagmati",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Central Development Region",
               "short_name" : "Central Development Region",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Nepal",
               "short_name" : "NP",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Kathmandu, Nepal",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 27.822074,
                  "lng" : 85.59104929999999
               },
               "southwest" : {
                  "lat" : 27.5661126,
                  "lng" : 85.18833169999999
               }
            },
            "location" : {
               "lat" : 27.7306785,
               "lng" : 85.37562489999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 27.822074,
                  "lng" : 85.59104929999999
               },
               "southwest" : {
                  "lat" : 27.5661126,
                  "lng" : 85.18833169999999
               }
            }
         },
         "place_id" : "ChIJe-_b6Swa6zkRZCTiGTJNiYs",
         "types" : [ "administrative_area_level_3", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "44600",
               "short_name" : "44600",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Bagmati",
               "short_name" : "Bagmati",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Central Development Region",
               "short_name" : "Central Development Region",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Nepal",
               "short_name" : "NP",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "44600, Nepal",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 27.8106869,
                  "lng" : 85.56890489999999
               },
               "southwest" : {
                  "lat" : 27.5464805,
                  "lng" : 85.1869583
               }
            },
            "location" : {
               "lat" : 27.7292955,
               "lng" : 85.3342973
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 27.8106869,
                  "lng" : 85.56890489999999
               },
               "southwest" : {
                  "lat" : 27.5464805,
                  "lng" : 85.1869583
               }
            }
         },
         "place_id" : "ChIJRb9Q-QwZ6zkRkfX_MNEacMA",
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Bagmati",
               "short_name" : "Bagmati",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Central Development Region",
               "short_name" : "Central Development Region",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Nepal",
               "short_name" : "NP",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Bagmati, Nepal",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 28.3420049,
                  "lng" : 86.06501
               },
               "southwest" : {
                  "lat" : 27.3182049,
                  "lng" : 84.62609499999999
               }
            },
            "location" : {
               "lat" : 28.0367577,
               "lng" : 85.4375574
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 28.3420049,
                  "lng" : 86.06501
               },
               "southwest" : {
                  "lat" : 27.3182049,
                  "lng" : 84.62609499999999
               }
            }
         },
         "place_id" : "ChIJ_wJv86_i6jkRESgJiF7pmkQ",
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Central Development Region",
               "short_name" : "Central Development Region",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Nepal",
               "short_name" : "NP",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Central Development Region, Nepal",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 28.3856697,
                  "lng" : 86.57675309999999
               },
               "southwest" : {
                  "lat" : 26.5699095,
                  "lng" : 83.921021
               }
            },
            "location" : {
               "lat" : 27.4695853,
               "lng" : 85.2722472
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 28.3856697,
                  "lng" : 86.57675309999999
               },
               "southwest" : {
                  "lat" : 26.5699095,
                  "lng" : 83.921021
               }
            }
         },
         "place_id" : "ChIJ0WrE8HZH6zkRAraslrlk0_M",
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Nepal",
               "short_name" : "NP",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Nepal",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 30.4469452,
                  "lng" : 88.20152569999999
               },
               "southwest" : {
                  "lat" : 26.3479661,
                  "lng" : 80.05846980000001
               }
            },
            "location" : {
               "lat" : 28.394857,
               "lng" : 84.12400799999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 30.4469452,
                  "lng" : 88.20152569999999
               },
               "southwest" : {
                  "lat" : 26.3479661,
                  "lng" : 80.05846980000001
               }
            }
         },
         "place_id" : "ChIJz2gufcfolTkR3obM0LyaojQ",
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}

1 个答案:

答案 0 :(得分:0)

由于Uber clone I've been building for our online course我已经得到了这个确切的代码,所以只需用你自己的地理编码网络服务密钥替换GOOGLE_GEOCODING_KEY

private static ConnectionRequest lastLocationRequest;
public static void nameMyCurrentLocation(Location l, SuccessCallback<String> name) {
    if(l == null) {
        return;
    }
    if(lastLocationRequest != null) {
        lastLocationRequest.kill();
    }
    lastLocationRequest = Rest.get("https://maps.googleapis.com/maps/api/geocode/json").
            queryParam("latlng", l.getLatitude() + "," + l.getLongitude()). 
            queryParam("key", GOOGLE_GEOCODING_KEY).
            queryParam("language", "en").
            queryParam("result_type", "street_address|point_of_interest").
            getAsJsonMap(callbackMap -> {
                Map data = callbackMap.getResponseData();
                if(data != null) {
                    List results = (List)data.get("results"); 
                    if(results != null && results.size() > 0) {
                        Map firstResult = (Map)results.get(0);
                        name.onSucess((String)firstResult.get("formatted_address")); 
                    }
                }
            });
}