Android Google Maps v2 - Keep marker in the center of map

匿名 (未验证) 提交于 2019-12-03 09:21:58

问题:

I'm a new Android developer working with Google Maps v2 right now.

I just need to let the user drag the map but the initial marker shouldn't move with it.

This is my layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="wrap_content"     android:layout_height="wrap_content" >      <fragment         android:id="@+id/map"         android:name="com.google.android.gms.maps.MapFragment"         android:layout_width="match_parent"         android:layout_height="match_parent" />      <LinearLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_marginLeft="2dp"         android:layout_marginRight="2dp"         android:layout_marginTop="20dp"         android:gravity="top|center_horizontal"         android:orientation="vertical" >          <EditText             android:id="@+id/edtDirection"             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:hint="@string/hint_buscar"             android:lines="1"             android:maxLines="1"             android:padding="5dp" />          <Button             android:id="@+id/btnSearch"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="@string/btn_buscar" />     </LinearLayout>  </RelativeLayout> 

And this is my MapActivity.java, which displays the map:

public class MapaActivity extends Activity {      private GoogleMap map;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         requestWindowFeature(Window.FEATURE_NO_TITLE);         setContentView(R.layout.activity_mapa);          if (map == null) {             map = ((MapFragment) getFragmentManager()                     .findFragmentById(R.id.map)).getMap();          map.addMarker(new MarkerOptions().position(new LatLng(-12.0000, -77.0000)).title("Origin"));          map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-12.0000, -77.0000), 14));          }      }  } 

Is there any method of Google Maps that I can use?

回答1:

You can simulate it. Instead of using a real Marker, center an ImageView on top of the MapView in a RelativeLayout.



回答2:

Try with this.. Instead of your code

//map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-12.0000, -77.0000), 14)); 

Use this code it will 'Keep marker in the center of map'

        map.animateCamera(CameraUpdateFactory.newLatLngZoom(                 new LatLng(LatitudeValue, LongitudeValue ), 14)); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!