我需要在google map
上只加载两个标记,但会加载多个标记。
以下是我的代码:
public class MainActivity extends FragmentActivity {
GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting reference to SupportMapFragment of the activity_main
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting Map for the SupportMapFragment
map = fm.getMap();
if (map != null) {
// Enable MyLocation Button in the Map
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
return;
}
map.setMyLocationEnabled(true);
//It is used to zoom when google first loading on your device.
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(23.0310791, 72.5954376), 11));
map.setMyLocationEnabled(true);
map.animateCamera(CameraUpdateFactory.zoomTo(11));
}
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng);
// Placing a marker on the touched position
map.addMarker(markerOptions);
}
});
}
}
我已经实施google map API
来加载地图,而上面是我正在尝试加载标记。它根据提供的纬度和长度加载标记,但我需要在我的谷歌地图上仅绘制两个标记。
答案 0 :(得分:1)
我的想法是,在地图上放置2个标记,并在每个地图上不断更改位置点击重要:将新位置设置为旧标记。
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
boolean swap;
Marker firstMarker = null, secondMarker = null;
@Override
public void onMapClick(LatLng latLng) {
if (firstMarker == null) {
MarkerOptions options = new MarkerOptions();
options.position(latLng);
firstMarker = map.addMarker(options);
return;
}
if (secondMarker == null) {
MarkerOptions options = new MarkerOptions();
options.position(latLng);
secondMarker = map.addMarker(options);
return;
}
swap = !swap;
if (swap) {
firstMarker.setPosition(latLng);
} else {
secondMarker.setPosition(latLng);
}
}
});