Google地图v2中的地图搜索栏

时间:2013-06-12 08:34:58

标签: android google-maps-api-2

我想在Google Maps v2的顶部添加一个“搜索栏”,如我在Google Play上剪下的这张小图片所示。

Google Map bar..

我该怎么做呢?感谢

1 个答案:

答案 0 :(得分:3)

这将是一个迟到的答案,但应该有这个非常普遍的需求的答案。

我们基本上需要做的是使用Google Geocoder在文本框中搜索用户提到的地址。

我们可以这样做,

Geocoder geo = new Geocoder(getBaseContext());
List<Address> gotAddresses = null;
try {
    gotAddresses = geocoder.getFromLocationName(locationName[0], 1);
} catch (IOException e) {
    e.printStackTrace();
}

此方法返回可用地址列表,在此代码中我们只接受1个地址,如果需要,我们可以通过更改上述方法中last参数的值来获取多个地址。然后,

Address address = (Address) gotAddresses.get(0);

LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());

String properAddress = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getCountryName());

mMap.addMarker(new MarkerOptions()
            .position(new LatLng(address.getLatitude(), address.getLongitude())).draggable(true)
            .title(properAddress)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));