我正在尝试在我的应用中添加Google地图。我在我的应用程序中成功显示了地图。
但问题是我没有得到我想要的位置。
static final LatLng vrrittihLocation = new LatLng(23.001779, 72.618946);
private GoogleMap googleMap;
if (googleMap == null) {
googleMap = ((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
}
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
MarkerOptions Mo = new MarkerOptions()
.position(vrrittihLocation)
.title("Vrrittih Global Recruitment Consulting")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_marker));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(vrrittihLocation, 18);
googleMap.animateCamera(cameraUpdate);
googleMap.addMarker(Mo);
我在这里错过了什么吗?
答案 0 :(得分:1)
标记应该在你的地图上,你没看到你想要的位置的原因也许你可以用下面的代码移动你的相机。
CameraPosition cameraPosition = new CameraPosition.Builder().target(vrrittihLocation)
.zoom(18)
.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
googleMap.animateCamera(cameraUpdate);
答案 1 :(得分:0)
通过 LocationManager 获取纬度和经度,如下所示,
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
latitude = location.getLatitude();
longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(latLng));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
}
和overiden方法onLocationChanged(...),
@Override
public void onLocationChanged(Location location) {
TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
latitude = location.getLatitude();
longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(latLng));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
}
希望这对你有帮助......
答案 2 :(得分:0)
googleMap.moveCamera(cameraUpdate);