Google Maps v2 - 设置我的位置并放大 [英] Google Maps v2 - set both my location and zoom in
问题描述
我的问题是,有没有人知道如何设置谷歌地图,以打开我的位置和放大视图?
My question is, does anyone know how to set google maps up, to open up both my location and in a zoomed-in view?
目前,主视图向非洲打开,一路缩小.
Currently, the main view opens up to Africa, all the way zoomed out.
所以我已经搜索了好几天了,我能找到的只有:
And so I have been searching for days now, and all I can find are:
1) 您不能在一张谷歌地图中为两件事(例如放大并转到我的位置)设置动画?所以如果我能在设置动画之前弄清楚如何设置缩放,那么这个问题就迎刃而解了.这往往是问题所在,您可以更改一个,但不能同时更改.
1) You cannot animate two things (like zoom in and go to my location) in one google map? So if I can figure out how to set the zoom before I set the animate, then this problem would be solved. That tends to be the issue, you can change one, but not both.
2) 我发现了其他可能有用的类,但没有关于如何设置代码以便该类可以操作谷歌地图的帮助.
2) I have found other classes that might be useful, but there is no help on how to set up the code so the class can manipulate the google map.
这是迄今为止我一直坚持的代码,有些有效,有些无效.有些我认为以后可能会有用.
This is the code I have been holding on to so far, some works, some do not. Some I thought might be useful later on.
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;
}
}
推荐答案
您不能在一张谷歌地图中为两件事(例如放大并转到我的位置)设置动画?
You cannot animate two things (like zoom in and go to my location) in one google map?
从编码的角度来看,您可以按顺序进行:
From a coding standpoint, you would do them sequentially:
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
-73.98180484771729));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
map.moveCamera(center);
map.animateCamera(zoom);
在这里,我先移动相机,然后为相机设置动画,尽管两者都可以是 animateCamera()
调用.GoogleMap
是否将这些合并为一个事件,我不能说,因为它进行得太快了.:-)
Here, I move the camera first, then animate the camera, though both could be animateCamera()
calls. Whether GoogleMap
consolidates these into a single event, I can't say, as it goes by too fast. :-)
这是我从中提取的示例项目上面的代码.
抱歉,这个答案有缺陷.请参阅 Rob 的回答,了解一种通过创建 CameraPosition
和然后从该 CameraPosition
创建一个 CameraUpdate
.
Sorry, this answer is flawed. See Rob's answer for a way to truly do this in one shot, by creating a CameraPosition
and then creating a CameraUpdate
from that CameraPosition
.
这篇关于Google Maps v2 - 设置我的位置并放大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!