大多数应用程序都使用Google Map,这些应用程序代表了Google Maps上的许多位置和标记。我们已经在Google地图上看到了多个位置的标记。在本文中,我们将研究Android中Google Maps上多个标记的实现。
我们将在本文中构建什么?
我们将构建一个简单的应用程序,在其中将在不同位置的地图上显示多个标记。下面给出了一个示例图像,以使您对本文中要做的事情有一个了解。注意,我们将使用Java语言实现该项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。确保在创建新项目时选择“地图活动”。
第2步:生成用于使用Google Maps的API密钥
要生成Maps的API密钥,您可以参考 如何生成用于在Android中使用Google Maps的API密钥。生成Google Maps的API密钥后。我们必须将此密钥添加到我们的项目中。要在我们的应用程序中添加此密钥,请导航至values文件夹> google_maps_api.xml文件,并在第23行中,您必须在YOUR_API_KEY位置添加API密钥。
第3步:向我们的地图添加多个标记
转到MapsActivity。 Java文件并参考以下代码。以下是MapsActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
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;
import java.util.ArrayList;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
// below are the latitude and longitude
// of 4 different locations.
LatLng sydney = new LatLng(-34, 151);
LatLng TamWorth = new LatLng(-31.083332, 150.916672);
LatLng NewCastle = new LatLng(-32.916668, 151.750000);
LatLng Brisbane = new LatLng(-27.470125, 153.021072);
// creating array list for adding all our locations.
private ArrayList locationArrayList;
@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);
// in below line we are initializing our array list.
locationArrayList = new ArrayList<>();
// on below line we are adding our
// locations in our array list.
locationArrayList.add(sydney);
locationArrayList.add(TamWorth);
locationArrayList.add(NewCastle);
locationArrayList.add(Brisbane);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// inside on map ready method
// we will be displaying all our markers.
// for adding markers we are running for loop and
// inside that we are drawing marker on our map.
for (int i = 0; i < locationArrayList.size(); i++) {
// below line is use to add marker to each location of our array list.
mMap.addMarker(new MarkerOptions().position(locationArrayList.get(i)).title("Marker"));
// below lin is use to zoom our camera on map.
mMap.animateCamera(CameraUpdateFactory.zoomTo(18.0f));
// below line is use to move our camera to the specific location.
mMap.moveCamera(CameraUpdateFactory.newLatLng(locationArrayList.get(i)));
}
}
}
添加此代码后。现在运行您的应用程序,并查看该应用程序的输出。
输出:
Note: In the Google Developer Console (https://console.developers.google.com), ensure that the “Google Maps Android API v2” is enabled. And also ensure that your API Key exists.