【问题标题】:Zooming Google Maps to the current Position将 Google 地图缩放到当前位置
【发布时间】:2016-01-06 08:42:49
【问题描述】:

我想缩放到当前位置时遇到问题。 我有一个 gps 按钮可以放大我,这很好用: 但实际上我不知道如何在加载地图时自动缩放。 这是我的 MainActivity 代码:

public class MainActivity extends Activity {

// Constant for defining latitude and longitude
//static final LatLng DerekPos = new LatLng(40 , -79);

// GoogleMap class
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


// verify we can interact with the Google Map
try {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().
                findFragmentById(R.id.map)).getMap();
    }
    // Show a satellite map with roads
    /* MAP_TYPE_NORMAL: Basic map with roads.
    MAP_TYPE_SATELLITE: Satellite view with roads.
    MAP_TYPE_TERRAIN: Terrain view without roads.
    */
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    // Place dot on current location
    googleMap.setMyLocationEnabled(true);

    // Turns traffic layer on
    googleMap.setTrafficEnabled(true);

    // Enables indoor maps
    googleMap.setIndoorEnabled(true);

    // Turns on 3D buildings
    googleMap.setBuildingsEnabled(true);

    // Show Zoom buttons
    googleMap.getUiSettings().setZoomControlsEnabled(true);

    // Create a marker in the map at a given position with a title
    //Marker marker = googleMap.addMarker(new MarkerOptions().
      //      position(DerekPos).title("Sie sind hier!"));

    //googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(DerekPos,16));


} catch (Exception e) {
    e.printStackTrace();
}
}

我得到一个按钮:

googleMap.setMyLocationEnabled(true);

当我点击此按钮时,它会将我移动到当前位置。 有人可以解释一下这是怎么发生的吗? 我想做的是在获取位置时放大而不按此按钮。

【问题讨论】:

    标签: android location maps


    【解决方案1】:

    我用那个方法来做到这一点:

    public static void ZoomLocMap(GoogleMap googleMap, SupportMapFragment map, double latitude, double Longitude, int nivelZoom) {
    
            try
            {
                googleMap = map.getMap();
    
                // Enabling MyLocation Layer of Google Map
                googleMap.setMyLocationEnabled(true);
    
                //acercar a localizacion animada
                LatLng latLng = new LatLng(latitude, Longitude);
    
                CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, nivelZoom);
                googleMap.animateCamera(cameraUpdate);
    
            }catch (NullPointerException e)
            {
                Log.i("LogsAndroid", "NullPointerException");
            }
        }
    

    【讨论】:

    • 我在右上角有一个按钮:googleMap.setMyLocationEnabled(true);当我按下此按钮时,它会放大我,但我不知道如何以及如何自动执行此操作.. 我的 MainActivity 中没有任何代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 2016-02-26
    • 2015-01-26
    • 1970-01-01
    相关资源
    最近更新 更多