[英]CameraPosition zoom and target doesn't work in Android 6
我正在做一個應用程序,該應用程序在讀取帶有此點的json后在地圖上顯示了一些POI。 我的應用程序在android 4.4上運行良好,但是當我在android 6(仿真器和真實設備)中運行時,相機無法按照我設置的水平縮放。 我沒有收到有關任何不推薦使用的命令的錯誤或警告。
這是MainActivity中的代碼
@Override
public void onMapReady(GoogleMap map) {
mapVar = map;
// MapWrapperLayout initialization
final MapWrapperLayout mapWrapperLayout = (MapWrapperLayout) findViewById(R.id.map_relative_layout);
// MapWrapperLayout initialization
mapWrapperLayout.init(map, getPixelsFromDp(this, 39 + 20));
this.infoWindow = (ViewGroup) getLayoutInflater().inflate(R.layout.info_window, null);
this.infoTitle = (TextView) infoWindow.findViewById(R.id.title);
this.infoSnippet = (TextView) infoWindow.findViewById(R.id.snippet);
this.infoButton = (Button) infoWindow.findViewById(R.id.button);
// Setting custom OnTouchListener which deals with the pressed state
// so it shows up
this.infoButtonListener = new OnInfoWindowElemTouchListener(infoButton,
getResources().getDrawable(R.drawable.round_but_green_sel), //btn_default_normal_holo_light
getResources().getDrawable(R.drawable.round_but_red_sel)) //btn_default_pressed_holo_light
{
@Override
protected void onClickConfirmed(View v, Marker marker) {
// Here we can perform some action triggered after clicking the button
//Toast.makeText(MainActivity.this, marker.getTitle() + "'s button clicked!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, dettaglio.class);
//EditText editText = (EditText) findViewById(R.id.edit_message);
String titolo = marker.getTitle();
String idPOI = allMarkersMapIds.get(marker);
String IMGPOI = allMarkersMapImg.get(marker);
String Desc = allMarkersMapDesc.get(marker);
String idUtentePOI = allMarkersMapidUtente.get(marker);
String idCategoria = allMarkersMapidCategoria.get(marker);
LATLON = marker.getPosition();
Bundle args = new Bundle();
args.putParcelable("coordinatePOI", LATLON);
intent.putExtra("bundle", args);
intent.putExtra(EXTRA_MESSAGE, titolo);
intent.putExtra(EXTRA_ID, idPOI);
intent.putExtra(EXTRA_IMG, IMGPOI);
intent.putExtra(EXTRA_Desc, Desc);
intent.putExtra("IDCATEGORIAPOI", idCategoria);
intent.putExtra("IDUTENTEPOI", idUtentePOI);
startActivity(intent);
}
};
this.infoButton.setOnTouchListener(infoButtonListener);
map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
// Setting up the infoWindow with current's marker info
infoTitle.setText(marker.getTitle());
infoSnippet.setText(marker.getSnippet());
infoButtonListener.setMarker(marker);
// We must call this to set the current marker and infoWindow references
// to the MapWrapperLayout
mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);
return infoWindow;
}
});
/// HERE I CENTER AND ZOOM THE CAMERA
CameraPosition googlePlex = CameraPosition.builder()
.target(new LatLng(latitude, longitude))
.zoom(15)
.bearing(0)
.tilt(45)
.build();
setUpMapIfNeeded(0, ricercaString);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newCameraPosition(googlePlex));
map.animateCamera(CameraUpdateFactory.newCameraPosition(googlePlex), 2000, null);
}
而且我在服務中做同樣的事情,監聽位置變化
@Override
public void onLocationChanged(Location location) {
listPOI = MainActivity.getArrayList();
// a bool value to understand if I can go ahead
goAhead = MainActivity.getGoAhead();
namesPOI = MainActivity.getNamePOI();
range = PrefActivity.getRange();
proximity = PrefActivity.getProximity();
//checks whether the user wants to be notified
if(proximity) {
//check if the list of near POI is loaded
if (goAhead) {
if (utility.isNear(location, listPOI, namesPOI, range)) {
POI = utility.getNamePOI();
String msg = "You're near " + POI;
tts1.tts.speak(msg, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
getLocation();
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(),location.getLongitude()));
MainActivity.mapVar.moveCamera(center);
CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
MainActivity.mapVar.animateCamera(zoom);
}
但是變焦仍然處於世界水平...有人可以弄清楚為什么嗎?
好的,我如下更改了onMapReady,現在它可以工作了。
@Override
public void onMapReady(GoogleMap map) {
mapVar = map;
// MapWrapperLayout initialization
CameraPosition googlePlex = CameraPosition.builder()
.target(new LatLng(latitude, longitude))
.zoom(15)
.bearing(0)
.tilt(45)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(googlePlex), 2000, null);
final MapWrapperLayout mapWrapperLayout = (MapWrapperLayout) findViewById(R.id.map_relative_layout);
// MapWrapperLayout initialization
mapWrapperLayout.init(map, getPixelsFromDp(this, 39 + 20));
this.infoWindow = (ViewGroup) getLayoutInflater().inflate(R.layout.info_window, null);
this.infoTitle = (TextView) infoWindow.findViewById(R.id.title);
this.infoSnippet = (TextView) infoWindow.findViewById(R.id.snippet);
this.infoButton = (Button) infoWindow.findViewById(R.id.button);
// Setting custom OnTouchListener which deals with the pressed state
// so it shows up
this.infoButtonListener = new OnInfoWindowElemTouchListener(infoButton,
getResources().getDrawable(R.drawable.round_but_green_sel), //btn_default_normal_holo_light
getResources().getDrawable(R.drawable.round_but_red_sel)) //btn_default_pressed_holo_light
{
@Override
protected void onClickConfirmed(View v, Marker marker) {
// Here we can perform some action triggered after clicking the button
//Toast.makeText(MainActivity.this, marker.getTitle() + "'s button clicked!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, dettaglio.class);
//EditText editText = (EditText) findViewById(R.id.edit_message);
String titolo = marker.getTitle();
String idPOI = allMarkersMapIds.get(marker);
String IMGPOI = allMarkersMapImg.get(marker);
String Desc = allMarkersMapDesc.get(marker);
String idUtentePOI = allMarkersMapidUtente.get(marker);
String idCategoria = allMarkersMapidCategoria.get(marker);
LATLON = marker.getPosition();
Bundle args = new Bundle();
args.putParcelable("coordinatePOI", LATLON);
intent.putExtra("bundle", args);
intent.putExtra(EXTRA_MESSAGE, titolo);
intent.putExtra(EXTRA_ID, idPOI);
intent.putExtra(EXTRA_IMG, IMGPOI);
intent.putExtra(EXTRA_Desc, Desc);
intent.putExtra("IDCATEGORIAPOI", idCategoria);
intent.putExtra("IDUTENTEPOI", idUtentePOI);
startActivity(intent);
}
};
this.infoButton.setOnTouchListener(infoButtonListener);
map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
// Setting up the infoWindow with current's marker info
infoTitle.setText(marker.getTitle());
infoSnippet.setText(marker.getSnippet());
infoButtonListener.setMarker(marker);
// We must call this to set the current marker and infoWindow references
// to the MapWrapperLayout
mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);
return infoWindow;
}
});
setUpMapIfNeeded(0, ricercaString);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
map.setMyLocationEnabled(true);
}
1。
代替animateCamera
> newCameraPosition
使用moveCamera
> newLatLngZoom
更換
CameraPosition cameraPosition = new CameraPosition.Builder().target(location).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
同
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 9));
在animateCamera
使用CancelableCallback
(可以解決的方法)
。
googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 5), new GoogleMap.CancelableCallback() { @Override public void onFinish() { CameraUpdate zout = CameraUpdateFactory.zoomBy(-3f); googleMap.animateCamera(zout); } @Override public void onCancel() { } });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.