gpt4 book ai didi

android - 如何在实时移动 map 时从android map 中心获取gps坐标

转载 作者:太空狗 更新时间:2023-10-29 14:11:49 26 4
gpt4 key购买 nike

我有什么:我有一张显示在 map 中心的图片

我正在尝试做的事情:当我移动 map 时,我想从 map 中心选择纬度/经度坐标并将其显示在 toast 中,当我移动 map 时它总是会改变


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();
}
});
}
});

最佳答案

首先:-

 mMap.getCameraPosition().target

上面的代码 fragment 将为您提供 Google map 的中心位置。在加载 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();
}
});
}
});

希望对你有所帮助!

关于android - 如何在实时移动 map 时从android map 中心获取gps坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27123902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com