我有什么: 我有一张显示在地图中心的图像
我想做什么: 当我移动地图时,我想从地图中心选择纬度/经度坐标并将其显示在一个托盘中,当我移动地图时它总是会改变
MainActivity.java
public class MainActivity extends Activity {
ImageView imageView1;
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1=(ImageView) findViewById(R.id.imageView1);
// Loading map
initilizeMap();
addMarker();
setCamera();
}
private void setCamera() {
//Fix the camera to the user location
MarkerOptions marker = new MarkerOptions().position(new LatLng(12.952782, 77.636247)).title("Your Location");
// HUE_RED color icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(12.952782, 77.636247)).zoom(16).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
private void addMarker() {
MarkerOptions marker = new MarkerOptions().position(new LatLng(12.952782, 77.636247));
// HUE_AZURE color icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
// adding marker
googleMap.addMarker(marker);
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
/**
* function to load map. If map is not created it will create it for you
* @throws Exception
* */
private void initilizeMap() {
try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
//Map Explicit settings//
//Map Rotate Gesture
googleMap.getUiSettings().setRotateGesturesEnabled(true);
//My Location Button
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
//Compass Functionality
googleMap.getUiSettings().setCompassEnabled(true);
//Zooming Functionality
googleMap.getUiSettings().setZoomGesturesEnabled(true);
//Zooming Buttons
googleMap.getUiSettings().setZoomControlsEnabled(true);
//Showing Current Location
googleMap.setMyLocationEnabled(true);
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(this,"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
}
}
}
activity_main.xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/marker" />
</FrameLayout>
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
LatLng latLng=googleMap.getCameraPosition().target;
Log.d("TAG", latLng.toString());
//Toast.makeText(this, latLng.toString(), Toast.LENGTH_LONG).show();
}
});
}
});
答案 0 :(得分:1)
首先: -
mMap.getCameraPosition().target
以上剪切代码将为您提供Google Map的中心位置。 在加载地图时做
googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
Log.e("TAG", googleMap.getCameraPosition().target.toString());
}
});
表示地图移动时: -
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
LatLng latLng= arg0.target;
Log.d("TAG", latLng.toString());
//Toast.makeText(this, latLng.toString(), Toast.LENGTH_LONG).show();
}
});
}
});
我希望它会帮助你......!