放大地图不同城市vs国家android

Mah*_*and 0 android google-maps-api-2

我使用下面的代码来搜索谷歌地图android

// An AsyncTask class for accessing the GeoCoding Web Service
    private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{

        @Override
        protected List<Address> doInBackground(String... locationName) {
            // Creating an instance of Geocoder class

            Log.d("bagibagi","doInBackground");
            Geocoder geocoder = new Geocoder(getBaseContext());
            List<Address> addresses = null;

            try {
                addresses = geocoder.getFromLocationName(locationName[0], 1);
            } catch (IOException e) {
                e.printStackTrace();
            }           
            return addresses;
        }


        @Override
        protected void onPostExecute(List<Address> addresses) {         

            Log.d("bagibagi","onPostExecute");
            search = "";
            if(addresses==null || addresses.size()==0){
                Toast.makeText(getBaseContext(), "No Location found OR you use this several time", Toast.LENGTH_SHORT).show();
            }
            else
            {

                // Adding Markers on Google Map for each matching address
                for(int i=0;i<addresses.size();i++){                

                    Address address = (Address) addresses.get(i);

                    // Creating an instance of GeoPoint, to display in Google Map
                    latLng = new LatLng(address.getLatitude(), address.getLongitude());


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


                    markerOptions = new MarkerOptions();
                    //markerOptions.icon(icon);
                    markerOptions.position(latLng);
                    markerOptions.title(addressText);

                    //map.addMarker(markerOptions);

                    Toast.makeText(getApplicationContext(), addressText, 1).show();

                    // Locate the first location

                    if(i==0){
                        CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(latLng)      // Sets the center of the map to Mountain View
                        .zoom(13) 
                        // Sets the zoom
                        //.bearing(90)                // Sets the orientation of the camera to east
                        //.tilt(30)                   // Sets the tilt of the camera to 30 degrees
                        .build();                   // Creates a CameraPosition from the builder
                        map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                    }   
                }   
            }


        }       
    }
Run Code Online (Sandbox Code Playgroud)

我如何改变.zoom(13)城市搜索或国家名称搜索.
例如,当用户搜索国家地图时,必须缩放小于搜索城市.

下面的图像显示你想要的东西.

在此输入图像描述

Nik*_*ski 6

您可以使用Google Places API获取特定地点的视口.

  1. 请求此网址:http://maps.googleapis.com/maps/api/geocode/json? address = Your-COUNTRY-OR-CITY& _nsor = true
  2. 在JSON响应中,您将找到以下键:
 geometry:{
      bounds:{
         northeast:{
            lat:40.501368,
            lng:-79.8657231
         },
         southwest:{
            lat:40.3613689,
            lng:-80.0952779
         }
      },
      location:{
         lat:40.44062479999999,
         lng:-79.9958864
      },
      location_type:"APPROXIMATE",
      viewport:{
         northeast:{
            lat:40.501368,
            lng:-79.8657231
         },
         southwest:{
            lat:40.3613689,
            lng:-80.0952779
         }
      }
   } 
  1. 去"几何"关键数据或"视口"
  2. 创建new LatLngBounds (LatLng southwestParsedCoordinate, LatLng northeastParsedCoordinate)对象并将摄像机移动到该绑定对象 mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10);