如何在Android中的谷歌地图中添加多个标记?

时间:2015-10-19 09:41:06

标签: android google-maps google-maps-api-3

我正在尝试在我的应用中添加多个标记。我尝试使用以下代码添加一个标记:

import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;

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.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 java.util.List;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    LocationManager mLocationManager;

    @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);

    }


    /**
     * 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;

        Location location = getLastKnownLocation();
        LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());

        mMap.addMarker(new MarkerOptions().position(userLocation).title("You are here."));

        mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
    }

    private Location getLastKnownLocation() {
        mLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
        List<String> providers = mLocationManager.getProviders(true);
        Location bestLocation = null;
        for (String provider : providers) {
            Location l = mLocationManager.getLastKnownLocation(provider);
            if (l == null) {
                continue;
            }
            if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
                // Found best last known location: %s", l);
                bestLocation = l;
            }
        }
        return bestLocation;
    }
}

以下代码正常。现在,我想添加多个标记。我也尝试在

中添加这些标记
     public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        Location location = getLastKnownLocation();
        LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());

        mMap.addMarker(new MarkerOptions().position(userLocation).title("You are here."));
         mMap.addMarker(new MarkerOptions().position(new LatLng(100,100)).title("You are here."));
mMap.addMarker(new MarkerOptions().position(new LatLng(150,150)).title("You are here."));

        mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
    }

但它没有正常工作。我只在屏幕上显示一个标记。

1 个答案:

答案 0 :(得分:0)

您给出的坐标是错误的。南半球和北半球的有效纬度范围分别为-90和+90。经度在-180和+180范围内,指定东西方位置。