我的问题是,有没有人知道如何设置谷歌 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 回答
从编码的角度来看,您可以按顺序执行:
在这里,我首先移动相机,然后为相机设置动画,尽管两者都可能是
animateCamera()
. 是否GoogleMap
将这些合并为一个单独的事件,我不能说,因为它太快了 . :-)Here is the sample project从中我拉了上面的代码 .
对不起,这个答案是有缺陷的 . 有关通过创建
CameraPosition
然后从CameraPosition
创建CameraUpdate
的方法,可以一次性真正执行此操作的方法,请参见Rob's answer .它's possible to change location, zoom, bearing and tilt all in one go. It'也可以设置
animateCamera()
呼叫的持续时间 .看看这里的文档:
https://developers.google.com/maps/documentation/android/views?hl=en-US#moving_the_camera
这是您问题的简单解决方案
试试这种方式 -
}
你也可以设置两个参数,如,
这会将您的 Map 定位在特定位置并进行缩放 . 我在设置我的 Map 时使用它 .
这是一个迟到的答案,但我认为这将有所帮助 . 使用此方法:
加载 Map 后使用此方法 . 干杯!
最简单的方法是使用CancelableCallback . 您应检查第一个操作是否完成,然后调用第二个操作:
1.在布局中添加xml代码以显示 Map .
2.启用谷歌 Map api然后得到下面的api关键位置 .
3.在oncreate中添加此代码 .
4.在oncreate之后添加此代码 . 然后使用放置在其中的标记访问当前位置
@ CommonsWare的答案实际上并没有起作用 . 我发现这个工作正常:
您无法在一个谷歌 Map 中为两件事(如放大和转到我的位置)制作动画 .
因此,使用移动和动画相机进行缩放