我无法获取已放置在地图上的标记的位置。将标记放在地图上后,我按一个按钮显示一个Toast,显示标记的纬度和经度,但应用程序强行关闭。
这是我的方法
public void markerLoc(View view)
{
LatLng lat = marker.getPosition();
double lat1 = lat.latitude;
double lng1 = lat.longitude;
Toast.makeText(getBaseContext(),"lat: "+lat1+" & long: "+lng1,Toast.LENGTH_SHORT).show();
}
Basically i want to add a marker to the map and on pressing the button the marker's location is to be shown. If the marker is moved the new location is to be shown on pressing the button.
Thanks in advance :)
my activity
public class Locate extends FragmentActivity implements LocationListener {
GoogleMap googleMap;
double latitude,longitude;
static int i=1;
GPSTracker gps;
Marker marker;
LatLng lat;
//Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locate);
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
if(status!= ConnectionResult.SUCCESS){
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}else {
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = fm.getMap();
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
}
}
@Override
public void onLocationChanged(Location location) {
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
Toast.makeText(getBaseContext(),"lat: "+location.getLatitude()+"long: "+location.getLatitude(),Toast.LENGTH_LONG).show();
latitude = location.getLatitude();
longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude );
}
@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
}
public void Dest(View view){
if(i==1) {
i=0;
gps = new GPSTracker(Locate.this);
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude+0.0005, longitude+0.0005))
.title("Your destination")
.draggable(true));
}else{
gps.showSettingsAlert();
}
}
else
Toast.makeText(getBaseContext(),"You can add only one destination spot",Toast.LENGTH_SHORT).show();
}
public void Clear(View view)
{
i=1;
googleMap.clear();
}
public void markerLoc(View view) {
if (marker != null) {
lat = marker.getPosition();
double lat1 = lat.latitude;
double lng1 = lat.longitude;
if(getBaseContext() != null) {
Toast.makeText(getBaseContext(), "lat: " + lat1 + " & long: " + lng1, Toast.LENGTH_SHORT).show();
}
}
}
} 我的xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location activity"
android:id="@+id/tv_location"
android:layout_gravity="center_horizontal" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="500dp"
class="com.google.android.gms.maps.SupportMapFragment" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add destination"
android:onClick="Dest"
android:id="@+id/dest" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="@+id/clear"
android:onClick="Clear"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MarkerLoc"
android:id="@+id/button"
android:onClick="markerLoc"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>
Dest按钮添加一个新标记,距离当前位置稍微远一点。如果我将标记移动到新位置,然后按markerLoc按钮,该位置似乎不会更改,它始终显示第一个位置。
答案 0 :(得分:0)
public void markerLoc(View view)
{
if(marker != null)
{
LatLng lat = marker.getPosition();
double lat1 = lat.latitude;
double lng1 = lat.longitude;
if(getBaseContext() != null)
{
Toast.makeText(getBaseContext(),"lat: "+lat1+" & long: "+lng1,Toast.LENGTH_SHORT).show();
}
}
}