使用我当前的位置启动地图应用

时间:2015-01-27 03:07:21

标签: android google-maps google-maps-android-api-2

这是我的活动:

import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.
    private LocationManager locationManager;
    private String provider;

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

        addMaracanazinho();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void addMaracanazinho()
    {
        LatLng pos = new LatLng(-34.642491, -58.642841);

        mMap.addMarker(new MarkerOptions()
                .title("Maracanazinho")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                .position(pos));


    }

    private void setUpMapIfNeeded()
    {
        // Do a null check to confirm that we have not already instantiated the map.
        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.setMyLocationEnabled(true);

            }
        }
    }



}

我希望我的应用程序以放大当前位置开始。我找了一些代码,但没有人有我想要的答案。有人能告诉我应该添加什么吗?

我找到了这个:How would I make my google maps app start with zoom on my current location 但我不明白如何把它放在我的代码中。 谢谢,抱歉我的英语不好。

3 个答案:

答案 0 :(得分:1)

使用animateCamera就像

一样
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
            latitude, longitude), 18.0f));

答案 1 :(得分:1)

将setUpMapIfNeeded方法更改为以下内容并添加其余代码。基本上,您需要使用系统的位置管理器注册LocationListener。你得到一个回调为onLocationChanged(Location l)。此位置l是用户位置,然后您将此新位置设置为地图。简单。

     private void setUpMapIfNeeded()
        {
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);

            // Do a null check to confirm that we have not already instantiated the map.
            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.setMyLocationEnabled(true);

                }
            }
        }

    private LocationListener locationListener = new MyLocationListener(){
     @Override
            public void onLocationChanged(Location location) {
                // called when the listener is notified with a location update from the GPS
    LatLng userPosition = new LatLng(location.getLatitude(),
                    location.getLongitude());
              if (mMap != null){
                mMap .moveCamera(CameraUpdateFactory.newLatLngZoom(userPosition,
                        15));
                }

            }

            @Override
            public void onProviderDisabled(String provider) {
               // called when the GPS provider is turned off (user turning off the GPS on the phone)

            }

            @Override
            public void onProviderEnabled(String provider) {
               // called when the GPS provider is turned on (user turning on the GPS on the phone)
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
               // called when the status of the GPS provider changes
            }
    };

答案 2 :(得分:1)

MapsActivity 文件中 - >更改 setUpMapIfNeeded()功能:

public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.
    private LocationManager locationManager;
    private String provider;

...

private void setUpMapIfNeeded()
    {
        // Do a null check to confirm that we have not already instantiated the map.
        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.setMyLocationEnabled(true);

                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

                // Create a criteria object to retrieve provider
                Criteria criteria = new Criteria();

                // Get the name of the best provider
                String provider = locationManager.getBestProvider(criteria, true);

                // Get Current Location
                Location myLocation = locationManager.getLastKnownLocation(provider);

                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(new LatLng(myLocation.getLatitude(), myLocation.getLongitude())) 
                        .zoom(14).build();

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

            }
        }
}

您也可以通过设置数字来设置缩放(此处缩放为14)。