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

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

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

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

所以我已经找了好几天了,我能找到的只有:

1) 您不能在一张谷歌地图中为两件事制作动画(例如放大并转到我的位置)?所以如果我能在设置动画之前弄清楚如何设置缩放,那么这个问题就解决了。这往往是问题所在,您可以更改一个,但不能同时更改两个。

2) 我找到了其他可能有用的类,但没有帮助说明如何设置代码以便该类可以操作 google 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;
}
}

最佳答案

You cannot animate two things (like zoom in and go to my location) in one google map?

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

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

    map.moveCamera(center);
    map.animateCamera(zoom);

在这里,我先移动相机,然后为相机设置动画,尽管两者都可能是 animateCamera() 调用。 GoogleMap 是否将这些合并为一个事件,我不能说,因为它进行得太快了。 :-)

Here is the sample project我从中提取了上面的代码。


对不起,这个答案有缺陷。见 Rob's answer一种真正一次性做到这一点的方法,方法是创建一个 CameraPosition,然后从该 CameraPosition 创建一个 CameraUpdate

关于android - Google Maps v2 - 设置我的位置并放大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14074129/

相关文章:

javascript - 如何在 ES6 中的 Vue.js 中使用谷歌地图

python - 谷歌地图 API 的命中率限制,但不知道为什么

android - 当用户靠近特定位置时向用户发送警报

Android检查没有 fragment 显示

java - Java 线程中的 super.run() 是什么意思?

java - 销毁或压低后,房间数据将无法持久保存,并重新回到 Activity 中

java - Google Maps API v2 显示空白 map

java - 静态字段什么时候初始化?

javascript - 打印时未显示 Google map

ios - Swift 中的 Google 自动完成查询