我正在使用Google Maps API开发应用程序。 我试图添加一个标记,用户在地图上按下该标记,并在用户创建的标记之间绘制一条线。
我尝试添加方法SetOnMapClickListener,但每次在虚拟设备中运行我的应用程序时,它都会立即崩溃。 我也尝试在OnCreate方法和OnMapReady中找到该方法,但这也不起作用。 我成功地添加了新的标记,但只是写了它们......
在我发现的一些教程中,我看到他们使用mapFragment.getMap()分配了谷歌地图对象,但我没有这种方法。 谢谢!
这是我的代码:
package com.example.user.testmap;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
double lit = latLng.latitude;
double lon = latLng.longitude;
mMap.addMarker(new MarkerOptions().position(new LatLng(lit, lon)));
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.addMarker(new MarkerOptions().position(new LatLng(20, 5)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(20, 5)));
}
}
答案 0 :(得分:0)
尝试删除mMap.setOnMapClickListener
中的onCreate()
,然后implements
GoogleMap.OnMapClickListener
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapClickListener {
private GoogleMap mMap;
private ArrayList<Marker> arrMarkerList;
private ArrayList<Polyline> arrPolylineList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMapClickListener(this);
arrMarkerList = new ArrayList<>();
arrPolylineList = new ArrayList<>();
}
@Override
public void onMapClick(LatLng latLng) {
double lit = latLng.latitude;
double lon = latLng.longitude;
arrMarkerList.add(mMap.addMarker(new MarkerOptions().position(new LatLng(lit, lon))));
// Removing the existing polyline to draw a new.
for (Polyline polyline : arrPolylineList) {
polyline.remove();
}
if (arrMarkerList.size() > 1) {
PolylineOptions polylineOptions = new PolylineOptions();
for (Marker marker : arrMarkerList) {
polylineOptions.add(marker.getPosition());
}
arrPolylineList.add(mMap.addPolyline(polylineOptions));
}
}
}