假设我们在地图上有一个中心标记,当我们用手指移动地图时,我们会看到当您超出该区域范围时,该标记将不再显示。那么,即使我们将地图移动到任何地方,我们如何都可以使其始终保持在地图中心的标记呢?有一个名为UBER的应用程序,它们提供此功能,您无需拖放标记即可移动地图,而标记仍保持其位置。
这里是屏幕截图:
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9WQUtneG0uanBnIn0=” alt =“ Uber地图标记”>
您可以看到,当我们移动地图时,绿色标记位于屏幕中央,它保留了其位置,但是当用户停止移动地图时,它会显示新的位置。我们该怎么做?
我有一个代码,当您拖放标记时会显示地址,如何再次显示地址,但是这次不通过拖动,移动地图呢?希望你能理解任何想法都会很棒。
public class MainActivity extends AbstractMapActivity implements
OnNavigationListener, OnInfoWindowClickListener,
OnMarkerDragListener {
private static final String STATE_NAV="nav";
private static final int[] MAP_TYPE_NAMES= { R.string.normal,
R.string.hybrid, R.string.satellite, R.string.terrain };
private static final int[] MAP_TYPES= { GoogleMap.MAP_TYPE_NORMAL,
GoogleMap.MAP_TYPE_HYBRID, GoogleMap.MAP_TYPE_SATELLITE,
GoogleMap.MAP_TYPE_TERRAIN };
private GoogleMap map=null;
String filterAddress = "";
Marker marker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (readyToGo()) {
setContentView(R.layout.activity_main);
SupportMapFragment mapFrag=
(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mapFrag.setRetainInstance(true);
initListNav();
map=mapFrag.getMap();
if (savedInstanceState == null) {
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(
41.003739,
28.999572));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(10);
map.moveCamera(center);
map.animateCamera(zoom);
addMarker(map, 41.003739, 28.999572, R.string.un, R.string.united_nations);
map.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
map.setOnInfoWindowClickListener(this);
map.setOnMarkerDragListener(this);
}
}
}
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
map.setMapType(MAP_TYPES[itemPosition]);
return(true);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt(STATE_NAV,
getSupportActionBar().getSelectedNavigationIndex());
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_NAV));
}
@Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(this, marker.getTitle(), Toast.LENGTH_LONG).show();
}
@Override
public void onMarkerDragStart(Marker marker) {
LatLng position=marker.getPosition();
Log.d(getClass().getSimpleName(), String.format("Drag from %f:%f",
position.latitude,
position.longitude));
}
@Override
public void onMarkerDrag(Marker marker) {
LatLng position=marker.getPosition();
Log.d(getClass().getSimpleName(),
String.format("Dragging to %f:%f", position.latitude,
position.longitude));
}
@Override
public void onMarkerDragEnd(Marker marker) {
LatLng position=marker.getPosition();
String filterAddress = "";
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
position.latitude,
position.longitude, 1);
if (addresses.size() > 0) {
for (int index = 0;
index < addresses.get(0).getMaxAddressLineIndex(); index++)
filterAddress += addresses.get(0).getAddressLine(index) + " ";
}
}catch (IOException ex) {
ex.printStackTrace();
}catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
Log.d(getClass().getSimpleName(),
String.format("Dragging to %f:%f", position.latitude,
position.longitude));
TextView myTextView = (TextView) findViewById(R.id.test);
myTextView.setText("Address is: " + filterAddress);
}
private void initListNav() {
ArrayList<String> items=new ArrayList<String>();
ArrayAdapter<String> nav=null;
ActionBar bar=getSupportActionBar();
for (int type : MAP_TYPE_NAMES) {
items.add(getString(type));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
nav=
new ArrayAdapter<String>(
bar.getThemedContext(),
android.R.layout.simple_spinner_item,
items);
}
else {
nav=
new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_item,
items);
}
nav.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
bar.setListNavigationCallbacks(nav, this);
}
private void addMarker(GoogleMap map, double lat, double lon,
int title, int snippet)
{
map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin))
.draggable(true));
}
}
这是xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/img_header" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<TextView
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_alignParentRight="true"
android:text="Address : "
android:textColor="#FF0000"
android:textSize="22sp" />
</RelativeLayout>
[我使用RelativeLayout作为片段的父对象,并在其中将地图和图片中心置于ImageView中(就像在超级或便捷的出租车应用中一样,将其作为居中地图标记):
<!-- Map and ImageView in center for simulating the map marker -->
<RelativeLayout
android:id="@+id/confirm_address_map_wrapper"
android:layout_width="match_parent"
android:layout_height="220dp">
<fragment
android:id="@+id/confirm_address_map_fragment"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
/>
<!-- Image View that acts as map marker notice centerInParent-->
<View
android:id="@+id/view"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_centerInParent="true"/>
<ImageView
android:id="@+id/confirm_address_map_custom_marker"
android:layout_width="@dimen/ICON_DEFAULT_SIZE"
android:layout_height="@dimen/ICON_DEFAULT_SIZE"
android:layout_above="@+id/view"
android:layout_centerHorizontal="true"
android:src="@mipmap/my_map_marker"/>
</RelativeLayout>
onMapReady()方法在我的活动中,我使用googleMap实例方法setOnCameraChangeListener()
,该方法可让我在使用地图时进行侦听,并根据摄像头位置的中心获取经度和纬度:
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
Log.i("centerLat",cameraPosition.target.latitude);
Log.i("centerLong",cameraPosition.target.longitude);
}
});
}
UPDATE:不推荐使用OnCameraChangeListener
。请使用OnCameraIdleListener
:[来源:https://stackoverflow.com/a/38732283/421467]
我无法获得Uber应用程序的效果,但是我可以执行类似的操作,也许它可以很好地利用GoogleMap.OnCameraChangeListener来解决您的问题。
活动:
public class MyActivity extends Activity implements OnCameraChangeListener {
...
@Override
public void onCameraChange(CameraPosition position) {
mMap.clear();
mMap.addMarker( new MarkerOptions()
.position( position.target )
.title( position.toString() )
);
}
...
}
问题是,在移动相机时未绘制标记。但是,似乎您已经创建了标记的叠加层。因此,通过覆盖它应该像Uber应用程序一样工作。
我希望这会有所帮助,即使这个问题还有几个月的历史,也许也会使未来的开发人员受益。
亲切的问候三木
如果您使用Fragment
显示Google地图,请将ImageView
放在Fragment
内以覆盖自定义标记,以便在移动地图时标记保持固定。请遵循xml文件的代码:
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" >
<ImageView
android:id="@+id/pin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@null"
android:src="@drawable/pin" />
</fragment>
在activity.java文件中,使用此:
ImageView pinButton = (ImageView) findViewById(R.id.pin);
使用setOnCameraChangeListener()
基于相机位置的中心获取经度或遵循@androidPlusPlus提供的答案
检查this答案
首先:将imageview添加到与中心相同的布局和位置。第二:使用以下方法获取地图中心的坐标
1_ get viewgroup ( FrameLayout ) in my case where the map fragment is located.
- FrameLayout myContainer = (FrameLayout) findViewById(R.id.content_frame);
int mapHeight = myContainer.getHeight();
int mapWidth = myContainer.getWidth();
2_ use setOnCameraChangeListener and on camera change get the coordinates:
LatLng center = mMap.getCameraPosition().target;
还有here是我的完整解决方案
mMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
@Override
public void onCameraMove() {
mCurrLocationMarker.remove();
LatLng midLatLng = mMap.getCameraPosition().target;
mCurrLocationMarker = mMap.addMarker(new MarkerOptions().title("I am here ").
position(midLatLng).icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_point));
}
});
mMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
@Override
public void onCameraIdle() {
mCurrLocationMarker.remove();
LatLng position = mCurrLocationMarker.getPosition();
mCurrLocationMarker = mMap.addMarker(new MarkerOptions().title("I am here ").
position(position).icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_source)));
getAddress(position.latitude, position.longitude);
}
});
对于使用osmdroid
map api的用户,请使用以下代码
marker = new Marker(map);
marker.setDraggable(true);
marker.setPosition(defPosition);
marker.setAnchor((float)0.5, (float)0.5);
map.getOverlays().add(marker);
////The overlay
Overlay mOverlay = new Overlay() {
@Override
public boolean onScroll(MotionEvent pEvent1, MotionEvent pEvent2, float pDistanceX, float pDistanceY, MapView pMapView) {
marker.setPosition(new GeoPoint((float) pMapView.getMapCenter().getLatitude(),
(float) pMapView.getMapCenter().getLongitude()));
return super.onScroll(pEvent1, pEvent2, pDistanceX, pDistanceY, pMapView);
}
};
map.getOverlays().add(mOverlay);