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

Dus*_*tin 122 api android google-maps google-maps-api-2

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

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

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

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

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

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

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;
}
}
Run Code Online (Sandbox Code Playgroud)

Com*_*are 200

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

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

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

    map.moveCamera(center);
    map.animateCamera(zoom);
Run Code Online (Sandbox Code Playgroud)

在这里,我首先移动相机,然后为相机设置动画,尽管两者都可以是animateCamera()通话.无论是GoogleMap将这些整合到一个单一的事件中,我都不能说,因为它过得太快了.:-)

这是我从中提取上述代码的示例项目.


对不起,这个答案是有缺陷的.请参阅Rob的回答,了解如何通过创建a CameraPosition然后CameraUpdate从中创建一个真正实现此目的的方法CameraPosition.


Rob*_*Rob 166

可以一次性改变位置,缩放,方位和倾斜.也可以设置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));
Run Code Online (Sandbox Code Playgroud)

看看这里的文档:

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


Ali*_*Ali 51

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

LatLng coordinate = new LatLng(lat, lng);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
map.animateCamera(yourLocation);
Run Code Online (Sandbox Code Playgroud)


Kho*_*aib 13

试试这种方式 -

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

}
Run Code Online (Sandbox Code Playgroud)

}


org*_*ero 7

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

mMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(21.000000, -101.400000) ,4) );
Run Code Online (Sandbox Code Playgroud)

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