我无法将Google地图相机移回初始位置。基本上,我在Actionbar上有一个主页按钮。如果用户在地图上滚动并点按“主页”按钮,我希望相机移回原来的位置。
我使用@Override
public boolean onOptionsItemSelected(MenuItem item)
{
LatLng initialLoc= mMap.getCameraPosition().target;
CameraUpdate update = CameraUpdateFactory.newLatLng(initialLoc);
CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
mMap.moveCamera(update);
mMap.animateCamera(zoom);
return true;
}
获取原始坐标,但相机不移动。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="org.test"
android:name="com.google.android.gms.maps.SupportMapFragment"
map:cameraTargetLat="xxx.xxx"
map:cameraTargetLng="xxx.xxx"
map:cameraZoom="15"
map:mapType="normal" />
</RelativeLayout>
map.xml:
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name: "Toggle Rows",
functionName: "toggleRows"
},{
name: "Hide Rows",
functionName: "hideRows"
},{
name: "Show Rows",
functionName: "showRows"
}];
sheet.addMenu("Script", entries);
};
function toggleRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var maxRows = sheet.getMaxRows();
var r = sheet.getRange(1, 1, maxRows+1);
var values = r.getValues();
var hideRanges = [];
var showRanges = [];
var toggleValue = null;
var startRow = null;
var tmp;
var len = values.length;
var i;
for (i = 0; i < len; i++) {
tmp = values[i][0];
if (startRow === null && (tmp === '--' || tmp === '||')) {
startRow = i + 1;
toggleValue = tmp;
} else if (startRow !== null && tmp !== toggleValue) {
if (toggleValue === '--') {
hideRanges.push([startRow, (i + 1) - startRow]);
} else {
showRanges.push([startRow, (i + 1) - startRow])
}
if (tmp === '--' || tmp === '||') {
startRow = i + 1;
toggleValue = tmp;
} else {
startRow = null;
}
}
}
var customRange = null;
var range = null;
i = hideRanges.length;
while (i--) {
customRange = hideRanges[i];
range = sheet.getRange(customRange[0], 1, customRange[1]);
range.setValue('||');
sheet.hideRows(customRange[0], customRange[1]);
}
i = showRanges.length;
while (i--) {
customRange = showRanges[i];
range = sheet.getRange(customRange[0], 1, customRange[1]);
range.setValue('--');
sheet.showRows(customRange[0], customRange[1]);
}
};
function hideRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var maxRows = sheet.getMaxRows();
var r = sheet.getRange(1, 1, maxRows+1);
var values = r.getValues();
var startRow = null;
var tmp;
var len = values.length;
var i;
for (i = 0; i < len; i++) {
tmp = values[i][0];
if (startRow === null && (tmp === '--' || tmp === '||')) {
startRow = i + 1;
} else if (startRow !== null && (tmp !== '--' && tmp !== '||')) {
var numRows = (i + 1) - startRow;
sheet.getRange(startRow, 1, numRows).setValue('||');
sheet.hideRows(startRow, numRows);
startRow = null;
}
}
};
function showRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var maxRows = sheet.getMaxRows();
var r = sheet.getRange(1, 1, maxRows+1);
var values = r.getValues();
var startRow = null;
var tmp;
var len = values.length;
var i;
for (i = 0; i < len; i++) {
tmp = values[i][0];
if (startRow === null && (tmp === '--' || tmp === '||')) {
startRow = i + 1;
} else if (startRow !== null && (tmp !== '--' && tmp !== '||')) {
var numRows = (i + 1) - startRow;
sheet.getRange(startRow, 1, numRows).setValue('--');
sheet.showRows(startRow, numRows);
startRow = null;
}
}
};
我错过了什么?
答案 0 :(得分:30)
每当用户选择该选项时,您正在使用
LatLng initialLoc= mMap.getCameraPosition().target;
获取所谓的初始位置,这是错误的!
mMap.getCameraPosition().target返回摄像头指向的位置。您应该根据其他代码将lat长期存储在活动的onCreate()
或其他某个位置,然后在onOptionItemSelected()
中使用相同的内容。
顺便说一句,您可以在单个语句中将zoom和lat long组合如下。
LatLng coordinate = new LatLng(lat, lng); //Store these lat lng values somewhere. These should be constant.
CameraUpdate location = CameraUpdateFactory.newLatLngZoom(
coordinate, 15);
mMap.animateCamera(location);
更新
我真的不知道是什么时候或者什么时候打电话给它。但是你可以使用相同的代码
LatLng initialLoc= mMap.getCameraPosition().target;
而是在onCreate()
或onResume()
中对此进行一次调用,然后将其存储在那里。然后下次在optionsItemSelected()
中使用这些值。虽然,为什么不简单地将您在xml中定义的值存储在java代码中然后使用它?
答案 1 :(得分:0)
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(totalAddress); //Here Total Address is address which you want to show on marker
mMap.clear();
markerOptions.icon(
BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
markerOptions.getPosition();
mCurrLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
答案 2 :(得分:0)
LatLng coordinate = new LatLng(latitude, longitude);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(coordinate);
markerOptions.title(placeName); //Here Total Address is address which you want to show on marker
mGoogleMap.clear();
markerOptions.icon(
BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
markerOptions.getPosition();
mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(coordinate));
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(15));