问题描述
我用下面的code创建一个地图和附加标记它。我还加入了标记听众,我需要得到标记位置的经度和纬度拖动后。
它确实是returnning我目前的位置不拖累后,标记的位置是什么。任何帮助,这部分?!
包com.example.mysample;
进口com.google.android.gms.common.GooglePlayServicesUtil;
进口com.google.android.gms.maps.CameraUpdateFactory;
进口com.google.android.gms.maps.GoogleMap;
进口com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
进口com.google.android.gms.maps.MapView;
进口com.google.android.gms.maps.Projection;
进口com.google.android.gms.maps.SupportMapFragment;
进口com.google.android.gms.maps.model.BitmapDescriptor;
进口com.google.android.gms.maps.model.BitmapDescriptorFactory;
进口com.google.android.gms.maps.model.LatLng;
进口com.google.android.gms.maps.model.Marker;
进口com.google.android.gms.maps.model.MarkerOptions;
进口com.google.android.maps.GeoPoint;
进口android.location.Location;
进口android.location.LocationListener;
进口android.os.Bundle;
进口android.app.Activity;
进口android.content.Context;
进口android.support.v4.app.FragmentActivity;
进口android.view.Menu;
进口android.view.MotionEvent;
进口android.widget.Toast;
进口android.graphics *。
公共类MainActivity扩展FragmentActivity实现LocationListener的{
GoogleMap的地图;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getApplicationContext());
地图=((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map))的GetMap()。
map.setMyLocationEnabled(真正的);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.addMarker(新MarkerOptions()
.position(新经纬度(0,0))
.title伪(标记)
.draggable(真)
是.snippet(你好)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
map.setOnMarkerDragListener(新OnMarkerDragListener(){
@覆盖
公共无效onMarkerDragStart(标记标记){
// TODO自动生成方法存根
//这里的code
Toast.makeText(MainActivity.this,拖动开始,
Toast.LENGTH_SHORT).show();
}
@覆盖
公共无效onMarkerDragEnd(标记标记){
// TODO自动生成方法存根
Toast.makeText(
MainActivity.this,
纬度+ map.getMyLocation()。getLatitude()+
+长+ map.getMyLocation()。getLongitude()
Toast.LENGTH_LONG).show();
的System.out.println(雅拉B2A
+ map.getMyLocation()getLatitude())。
}
@覆盖
公共无效onMarkerDrag(标记标记){
// TODO自动生成方法存根
// Toast.makeText(MainActivity.this,拖,
// Toast.LENGTH_SHORT).show();
的System.out.println(Draagging);
}
});
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
公共布尔onMarkerClick(最终标记标记){
如果(marker.equals(地图)){
//处理请点击这里
// map.getMyLocation();
的System.out.println(点击);
。双纬度= map.getMyLocation()getLatitude();
的System.out.println(纬度+ LAT);
Toast.makeText(MainActivity.this,
当前位置+ map.getMyLocation()。getLatitude()
Toast.LENGTH_SHORT).show();
}
返回true;
}
@覆盖
公共无效onLocationChanged(位置定位){
// TODO自动生成方法存根
}
@覆盖
公共无效onProviderDisabled(字符串提供商){
// TODO自动生成方法存根
}
@覆盖
公共无效onProviderEnabled(字符串提供商){
// TODO自动生成方法存根
}
@覆盖
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
// TODO自动生成方法存根
}
}
看看你的code此功能。
@覆盖
公共无效onMarkerDragEnd(标记标记){
// TODO自动生成方法存根
Toast.makeText(
MainActivity.this,
纬度+ map.getMyLocation()。getLatitude()+
+长+ map.getMyLocation()。getLongitude()
Toast.LENGTH_LONG).show();
的System.out.println(雅拉B2A
+ map.getMyLocation()getLatitude())。
}
在这里,你正在试图让地图这是不对的,你应该得到你拖标记的位置,您的当前位置。你已经在这里标记的对象。用它来得到这个draged标记的位置的位置。
经纬度位置= marker.getPosition(); //
Toast.makeText(
MainActivity.this,
纬度+ position.latitude +
+长+ position.longitude,
Toast.LENGTH_LONG).show();
I'm using the following code to create a map and attach a marker to it. I'm also adding a marker listener where I need to get the longitude and latitude of the marker position after dragging.
What it does is returnning my current location not the location of the marker after drag.Any help with this part?!
package com.example.mysample;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.Projection;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.maps.GeoPoint;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.Toast;
import android.graphics.*;
public class MainActivity extends FragmentActivity implements LocationListener {
GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getApplicationContext());
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Marker")
.draggable(true)
.snippet("Hello")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
map.setOnMarkerDragListener(new OnMarkerDragListener() {
@Override
public void onMarkerDragStart(Marker marker) {
// TODO Auto-generated method stub
// Here your code
Toast.makeText(MainActivity.this, "Dragging Start",
Toast.LENGTH_SHORT).show();
}
@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());
}
@Override
public void onMarkerDrag(Marker marker) {
// TODO Auto-generated method stub
// Toast.makeText(MainActivity.this, "Dragging",
// Toast.LENGTH_SHORT).show();
System.out.println("Draagging");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onMarkerClick(final Marker marker) {
if (marker.equals(map)) {
// handle click here
// map.getMyLocation();
System.out.println("Clicked");
double lat = map.getMyLocation().getLatitude();
System.out.println("Lat" + lat);
Toast.makeText(MainActivity.this,
"Current location " + map.getMyLocation().getLatitude(),
Toast.LENGTH_SHORT).show();
}
return true;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Take a look at this function in your code.
@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());
}
Here you are trying to get your current location on map which is wrong you should get location of marker that you dragged. You already have "marker" object here. Use that to get location of this draged marker's location.
LatLng position = marker.getPosition(); //
Toast.makeText(
MainActivity.this,
"Lat " + position.latitude + " "
+ "Long " + position.longitude,
Toast.LENGTH_LONG).show();
这篇关于获取纬度和标记的经度的谷歌地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!