英文:
How to save longitude and latitude from the marker i set on the map
问题
我知道如何根据坐标设置位置,当地图活动启动时,它会跳转到这些坐标,但我希望用户能够在地图上设置一个标记,并将坐标保存到一些变量中,但我不知道如何做。请指点我正确的方向。
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions()
.position(sydney)
.title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
为了完整地查看我的代码,我添加了一些图片。我很抱歉,我对地图这方面还是很新手。每一条评论都将帮助我更多地学习这个。
英文:
I know how to set location from coordinates and when the maps activity starts it will jump to those coordinates but i want the user to be able to set a marker on the map and save the coordinates into some variables but i have no idea how to do that. Kindly point me in the right direction.
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions()
.position(sydney)
.title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
Added some pics for full view of my code. I am sorry i m really new into this maps stuff. Every comment will help me learn more about this.
答案1
得分: 0
设置一个全局变量来表示您的活动
private LatLng myLatLng;
接下来,您需要为地图设置一个标记拖动监听器
map.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker marker) {
}
@Override
public void onMarkerDrag(Marker marker) {
}
@Override
public void onMarkerDragEnd(Marker marker) {
myLatLng = marker.getPosition();
}
});
如果您想将标记保存为一个变量...
private Marker myMarker;
private LatLng myLatLng;
myMarker = mMap.addMarker(new MarkerOptions()
.position(sydney)
.title("Marker in Sydney"));
myLatLng = myMarker.getPosition();
英文:
Set up a global variable for your activity
private LatLng myLatLng;
Next, you need to set up a marker drag listener for your map
map.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker marker) {
}
@Override
public void onMarkerDrag(Marker marker) {
}
@Override
public void onMarkerDragEnd(Marker marker) {
myLatLng = marker.getPosition();
}
});
If you want to save the marker as a variable...
private Marker myMarker;
private LatLng myLatLng;
myMarker = mMap.addMarker(new MarkerOptions()
.position(sydney)
.title("Marker in Sydney"));
myLatLng = myMarker.getPosition();
答案2
得分: 0
仅将您的最后一行替换为:
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sydney, 15));
英文:
Just replace your last line with
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sydney, 15));
答案3
得分: 0
首先在地图上添加标记
private void setUpMap()
{
.......
googleMap.setOnMarkerClickListener(this);
myMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("我的标记")
.snippet("这是我的标记!")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
......
}
然后使用以下代码获取标记位置
@Override
public void onMarkerDragEnd(Marker marker) {
// TODO Auto-generated method stub
Toast.makeText(
MainActivity.this,
"纬度 " + map.getMyLocation().getLatitude() + " "
+ "经度 " + map.getMyLocation().getLongitude(),
Toast.LENGTH_LONG).show();
System.out.println("yalla b2a "
+ map.getMyLocation().getLatitude());
}
英文:
First add marker on map
private void setUpMap()
{
.......
googleMap.setOnMarkerClickListener(this);
myMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my spot!")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
......
}
then use this code to get marker location
@Override
public void onMarkerDragEnd(Marker marker) {
// TODO Auto-generated method stub
Toast.makeText(
MainActivity.this,
"Lat " + map.getMyLocation().getLatitude() + " "
+ "Long " + map.getMyLocation().getLongitude(),
Toast.LENGTH_LONG).show();
System.out.println("yalla b2a "
+ map.getMyLocation().getLatitude());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论