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

时间:2014-05-05 14:27:59

标签: android google-maps-api-2

我使用下面的代码在google map 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));
                    }   
                }   
            }


        }       
    }

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

图片下方的

会显示我想要的内容。

enter image description here

1 个答案:

答案 0 :(得分:6)

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

  1. 请求此网址:http://maps.googleapis.com/maps/api/geocode/json?address=YOUR-COUNTRY-OR-CITY&sensor=true
  2. 在JSON响应中,您将找到以下键:
  3.  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);