2015-10-19 120 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