首页 文章

Google Maps v2 - 设置我的位置并放大

提问于
浏览
119

我的问题是,有没有人知道如何设置谷歌 Map ,打开我的位置和放大视图?

目前,主视图向非洲开放,一路缩小 .

所以我一直在寻找几天,我能找到的是:

1)你不能在一个谷歌 Map 中动画两件事(比如放大并转到我的位置)?因此,如果我可以在设置动画之前弄清楚如何设置缩放,那么这个问题就可以解决了 . 这往往是问题,你可以改变一个,但不能两者兼而有之 .

2)我发现其他可能有用的类,但是如何设置代码没有任何帮助,因此类可以操纵谷歌 Map .

这是迄今为止我一直坚持的代码,有些是作品,有些则不然 . 我认为有些可能会在以后有用 .

package com.MYWEBSITE.www;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity {
private GoogleMap map;  

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);

    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    map.setMyLocationEnabled(true);

    //LocationSource a = (LocationSource) getSystemService(Context.LOCATION_SERVICE);
    //LocationManager b = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    //map.setLocationSource(a);

    Criteria criteria = new Criteria();
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    String provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);
    double lat =  location.getLatitude();
    double lng = location.getLongitude();
    LatLng coordinate = new LatLng(lat, lng);

    //CameraPosition.Builder x = CameraPosition.builder();
    //x.target(coordinate);
    //x.zoom(13);

    //Projection proj = map.getProjection();
    //Point focus = proj.toScreenLocation(coordinate);

    //map.animateCamera(CameraUpdateFactory.newLatLng(coordinate));
    map.animateCamera(CameraUpdateFactory.zoomBy(13));
    //map.moveCamera(CameraUpdateFactory.newLatLng(coordinate));


    ////LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
}
}

11 回答

  • 51

    你不能在一个谷歌 Map 中制作两件事(比如放大并转到我的位置)?

    从编码的角度来看,您可以按顺序执行:

    CameraUpdate center=
            CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
                                                     -73.98180484771729));
        CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
    
        map.moveCamera(center);
        map.animateCamera(zoom);
    

    在这里,我首先移动相机,然后为相机设置动画,尽管两者都可能是 animateCamera() . 是否 GoogleMap 将这些合并为一个单独的事件,我不能说,因为它太快了 . :-)

    Here is the sample project从中我拉了上面的代码 .


    对不起,这个答案是有缺陷的 . 有关通过创建 CameraPosition 然后从 CameraPosition 创建 CameraUpdate 的方法,可以一次性真正执行此操作的方法,请参见Rob's answer .

  • 13

    它's possible to change location, zoom, bearing and tilt all in one go. It'也可以设置 animateCamera() 呼叫的持续时间 .

    CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(MOUNTAIN_VIEW)      // Sets the center of the map to Mountain View
        .zoom(17)                   // 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));
    

    看看这里的文档:

    https://developers.google.com/maps/documentation/android/views?hl=en-US#moving_the_camera

  • -2

    这是您问题的简单解决方案

    LatLng coordinate = new LatLng(lat, lng);
    CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
    map.animateCamera(yourLocation);
    
  • 0

    试试这种方式 -

    public class SummaryMapActivity extends FragmentActivity implements LocationListener{
    
     private GoogleMap mMap;
     private LocationManager locationManager;
     private static final long MIN_TIME = 400;
     private static final float MIN_DISTANCE = 1000;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.summary_mapview);
    
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
            }
        }
        mMap.setMyLocationEnabled(true);
    
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this); 
    
    
    }
    
    @Override
    public void onLocationChanged(Location location) {
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);
        mMap.animateCamera(cameraUpdate);
        locationManager.removeUpdates(this);
    
    }
    
    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    
    }
    

    }

  • 1

    你也可以设置两个参数,如,

    mMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(21.000000, -101.400000) ,4) );
    

    这会将您的 Map 定位在特定位置并进行缩放 . 我在设置我的 Map 时使用它 .

  • 194

    这是一个迟到的答案,但我认为这将有所帮助 . 使用此方法:

    protected void zoomMapInitial(LatLng finalPlace, LatLng currenLoc) {
        try {
            int padding = 200; // Space (in px) between bounding box edges and view edges (applied to all four sides of the bounding box)
            LatLngBounds.Builder bc = new LatLngBounds.Builder();
    
            bc.include(finalPlace);
            bc.include(currenLoc);
            googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bc.build(), padding));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    加载 Map 后使用此方法 . 干杯!

  • 3

    最简单的方法是使用CancelableCallback . 您应检查第一个操作是否完成,然后调用第二个操作:

    mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, size.x, height, 0), new CancelableCallback() {
    
                    @Override
                    public void onFinish() {
                        CameraUpdate cu_scroll = CameraUpdateFactory.scrollBy(0, 500);
                        mMap.animateCamera(cu_scroll);
                    }
    
                    @Override
                    public void onCancel() {
                    }
                });
    
  • 7

    1.在布局中添加xml代码以显示 Map .

    2.启用谷歌 Map api然后得到下面的api关键位置 .

    <fragment
                       android:id="@+id/map"
                   android:name="com.google.android.gms.maps.MapFragment"
                        android:layout_width="match_parent"
                        android:value="ADD-API-KEY"
                        android:layout_height="250dp"
                        tools:layout="@layout/newmaplayout" />
            <ImageView
                android:id="@+id/transparent_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@color/transparent" />
    

    3.在oncreate中添加此代码 .

    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(UpadateProfile.this);
    

    4.在oncreate之后添加此代码 . 然后使用放置在其中的标记访问当前位置

    @Override
    public void onMapReady(GoogleMap rmap) {
        DO WHATEVER YOU WANT WITH GOOGLEMAP
        map = rmap;
        setUpMap();
    }
    public void setUpMap() {
        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
      map.setMyLocationEnabled(true);
        map.setTrafficEnabled(true);
        map.setIndoorEnabled(true);
        map.getCameraPosition();
        map.setBuildingsEnabled(true);
        map.getUiSettings().setZoomControlsEnabled(true);
        markerOptions = new MarkerOptions();
        markerOptions.title("Outlet Location");
        map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng point) {
                map.clear();
                markerOptions.position(point);
                map.animateCamera(CameraUpdateFactory.newLatLng(point));
                map.addMarker(markerOptions);
                String all_vals = String.valueOf(point);
                String[] separated = all_vals.split(":");
                String latlng[] = separated[1].split(",");
                MyLat = Double.parseDouble(latlng[0].trim().substring(1));
                MyLong = Double.parseDouble(latlng[1].substring(0,latlng[1].length()-1));
                markerOptions.title("Outlet Location");
                getLocation(MyLat,MyLong);
            }
        });
    }
    public void getLocation(double lat, double lng) {
        Geocoder geocoder = new Geocoder(UpadateProfile.this, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
       } catch (IOException e) {
             TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this,e.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onLocationChanged(Location location) {
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    @Override
    public void onProviderEnabled(String provider) {
    }
    @Override
    public void onProviderDisabled(String provider) {
    }
    
  • 0

    @ CommonsWare的答案实际上并没有起作用 . 我发现这个工作正常:

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-33.88,151.21), 15));
    
  • 1

    您无法在一个谷歌 Map 中为两件事(如放大和转到我的位置)制作动画 .
    因此,使用移动和动画相机进行缩放

    googleMapVar.moveCamera(CameraUpdateFactory.newLatLng(LocLtdLgdVar));
    googleMapVar.animateCamera(CameraUpdateFactory.zoomTo(10));
    
  • 158
    gmap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(new LatLng(9.491327, 76.571404), 10, 30, 0)));
    

相关问题