【问题标题】:Center Google Maps to current user position将 Google 地图居中到当前用户位置
【发布时间】:2016-02-26 21:25:06
【问题描述】:

我创建了一个 Android 活动来显示 Google 地图。 我会将地图居中到当前用户位置,但我不知道该怎么做。

这是我当前的代码:

Location location = getMyLocation();
LatLng userPos = new LatLng(location.getLatitude(), location.getLongitude());

map.animateCamera(CameraUpdateFactory.newLatLngZoom(userPos, zoomLevel));

CameraPosition cameraPosition = new CameraPosition.Builder()
                                    .target(userPos)
                                    .zoom(17)
                                    .bearing(90)
                                    .tilt(40)
                                    .build();

map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

此代码的来源是 answer(我从中复制了 getMyLocation())和 solution,但它们不起作用。在这两种情况下它都不起作用:中心没有设置,奇怪的是缩放级别也没有设置。

如果我只使用这段代码(没有当前用户位置):

map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(42.564241, 12.22759), zoomLevel));

即使设置了缩放级别,它也可以正常工作。

可能是什么问题?

【问题讨论】:

    标签: android google-maps dictionary position location


    【解决方案1】:

    确保您拥有以下权限: <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 然后进行一些活动并注册一个 LocationListener

    package com.example.location;
    
    import android.content.Context;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.view.View;
    import com.actionbarsherlock.app.SherlockFragmentActivity;
    import com.google.android.gms.maps.CameraUpdate;
    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;
    
    public class LocationActivity extends SherlockFragmentActivity implements 
    LocationListener     {       
    private GoogleMap map;    
    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.map);
    map = ((SupportMapFragment)     
    getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    
    locationManager = (LocationManager)   
    getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,   
    MIN_TIME, MIN_DISTANCE, this); //You can also use   
    LocationManager.GPS_PROVIDER and LocationManager.PASSIVE_PROVIDER        
     }
     @Override
    public void onLocationChanged(Location location) {
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
    map.animateCamera(cameraUpdate);
    locationManager.removeUpdates(this);
    }
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) { }
    
    @Override
    public void onProviderEnabled(String provider) { }
    
    @Override
    public void onProviderDisabled(String provider) { }
    }'
    

    【讨论】:

      猜你喜欢
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 2013-06-27
      相关资源
      最近更新 更多