我想在Google Map V2上搜索区域名称

时间:2014-11-28 10:04:31

标签: android google-maps geocoding

我问了一个关于如何在android应用程序中获得Google Map V2的问题。现在我想在Google Map上添加一项功能。任何人都可以给我一个很好的答案,我试图用Geocode来解决它。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您可以将此代码放入活动的onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.show_map, menu);

    SearchManager searchManger =  (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =  (SearchView) menu.findItem(R.id.action_search).getActionView();

    searchView.setSearchableInfo(searchManger.getSearchableInfo(getComponentName()));
    //searchView.setIconifiedByDefault(false);

    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener()
    {

        @Override
        public boolean onQueryTextSubmit(String newText) 
        {
            return true;
        }

        @Override
        public boolean onQueryTextChange(String query) 
        {
            Geocoder geocoder = new Geocoder(CreateReminderActivity.this);  

            List<Address> addresses;

                    String areaname = edLocation.getText().toString();

                    addresses = geocoder.getFromLocationName(areaname, 1);

                    if(addresses.size() > 0) {

                        double latitude= addresses.get(0).getLatitude();

                        double longitude= addresses.get(0).getLongitude();

                        insLat = latitude;

                        insLong = longitude;

                        String statename = addresses.get(0).getAdminArea();

                        googleMap.addMarker(new MarkerOptions()
                        .title(areaname)
                        .snippet("SNIPPET")
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
                        .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
                        .position(new LatLng(latitude, longitude)));

                        LatLng latLng = new LatLng(latitude, longitude);

                        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

                     // Zoom in the Google Map
                     googleMap.animateCamera(CameraUpdateFactory.zoomTo(10));

                     Toast.makeText(getApplicationContext(), statename, 20).show(); 

            return true;
        }
    };


    searchView.setOnQueryTextListener(queryTextListener);



    return true;
}