我的问题是,有谁知道如何设置谷歌地图,打开我的位置和放大视图?谷歌地图v2 - 设置我的位置和放大
目前,主视图向非洲开放,一路缩小。
所以我一直在寻找几天了,所有我能看到的是:
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;
}
}
不应该接受的答案。 Mubeen1992答案是正确的。 – Matthew
@AlanHaverty:我在答案中链接到的示例应用程序包含该代码。它运行得很好。我现在只是在运行Android 7.0的Nexus 9和运行Android 6.0.1的Nexus 6P上进行测试。它几年来运行得非常好。它*不会*一次完成,我同意Rob's是一个优越的答案,正如我在答案中所指出的那样。如果您认为您有证据表明此代码无效,请在某处发布示例项目。 – CommonsWare
@CommonsWare我的道歉,在测试你的代码时,我的地图的左上角似乎是用lat lng设置的。我认为你错了.newLatLng()做了什么。我不确定它为什么关闭,它一定是我的最终结果,api文档证明了你的情况,对于你的答案开启触发感到抱歉! 删除我的第一条评论以避免混淆。 API来源:https://developers.google.com/android/reference/com/google/android/gms/maps/CameraUpdateFactory.html#newLatLng(com.google.android .gms.maps.model。LatLng) –