2015-10-19 117 views
0

我想在我的应用程序中添加多个标记。我曾尝试使用以下代码添加一个标记:如何在Android中的谷歌地图添加多个标记?

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

但它不能正常工作。屏幕上只显示一个标记。

+0

哪些代码不工作?首先正确描述你的问题 –

+0

我没有在屏幕上看到多个标记。 – learner

+0

我只看到一个标记? –

回答

0

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

+0

我需要修改地理概念。 :d – learner