我正在尝试在我的位置启动谷歌地图,但我不能,但我可以在你想要它作为初始的位置放置一个标记。我需要你的帮助。我在这里搜索了很多,但没有任何对我有用。
最好的问候
代码
@TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
getSreenDimanstions();
fragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
map = fragment.getMap();
bNavigation = (Button) findViewById(R.id.bNavigation);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
miPosicion = new LatLng(latitude, longitude);
map.addMarker(new MarkerOptions().position(miPosicion).title("Start"));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 13));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude())) // Sets the center of the map to location user
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
obtenerItems(map);
}
答案 0 :(得分:0)
我使用this documentation使用:
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 17.0f);
theMap.animateCamera(update);
答案 1 :(得分:0)
如果您想获得确切的当前位置,最好还是打电话
requestLocationUpdates(..)
而不是
getLastKnownLocation(..)
然后实施LocationListener
来收听位置变化并采取相应措施。有关LocationListener的更多信息,请参阅:
http://developer.android.com/reference/android/location/LocationListener.html
并且旁注:您似乎已初始化了两个LatLng
类型(latLng
和miPosicion
)并且具有完全相同的值,并且尚未完全使用。