reg*_*eme 31 android google-maps google-maps-markers
假设我们在地图上有一个居中的标记,当我们用手指移动地图时,我们可以看到当你离开该区域时,标记将不再显示.那么我们怎样才能制作一个始终位于地图中心的标记,尽管我们将地图移动到任何地方?有一个名为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));
}
}
Run Code Online (Sandbox Code Playgroud)
这是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>
Run Code Online (Sandbox Code Playgroud)
Com*_*ode 33
我使用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>
Run Code Online (Sandbox Code Playgroud)
在我的活动中的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);
}
});
}
Run Code Online (Sandbox Code Playgroud)
更新:
OnCameraChangeListener
已弃用.请使用OnCameraIdleListener
:
[来源:https://stackoverflow.com/a/38732283/421467]
小智 10
我无法获得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() )
);
}
...
}
Run Code Online (Sandbox Code Playgroud)
问题是,在移动相机时不会绘制标记.但是,似乎您已经创建了标记的叠加层.所以使用叠加层它应该像Uber应用程序一样工作.
我希望这会有所帮助,即使是几个月前的问题,也许这对未来的开发者有利.
亲切的问候三木
归档时间: |
|
查看次数: |
32676 次 |
最近记录: |