2017-02-09 1 views
0

Ich habe eine Anwendung, in der ich den aktuellen Standort des Benutzers und den Marker an einem aktuellen Standort hinzufügen und es funktioniert perfekt, aber ich habe etwas lat und lon und ich möchte auf der Karte mit Marker zeigenHinzufügen von Marker auf einer Karte Android

Lat, Lon ist in workshopList

Mein Code:

public class FindServiceProviders extends AppCompatActivity implements OnMapReadyCallback, 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     LocationListener { 

    ArrayList<ServiceProviderItem> workshopList; 
    GoogleMap mGoogleMap; 
    SupportMapFragment mapFrag; 
    LocationRequest mLocationRequest; 
    GoogleApiClient mGoogleApiClient; 
    Location mLastLocation; 
    Marker mCurrLocationMarker; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

      setContentView(R.layout.activity_find_service_providers); 
      mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
      mapFrag.getMapAsync(this); 
      init(); 
    } 


    private void init() 
    { 
     setToolBar(); 
     workshopList = new ArrayList<ServiceProviderItem>(); 
     mRecyclerView=(RecyclerView)findViewById(R.id.recyclerView); 
     mRecyclerView.setHasFixedSize(true); 
     mLayoutManager=new LinearLayoutManager(this); 
     mRecyclerView.setLayoutManager(mLayoutManager); 
     fetchServiceProviders(); 
     mAdapter=new ServiceProviderAdapter(this,workshopList); 

    } 
    @Override 
    public void onPause() { 
     super.onPause(); 

     //stop location updates when Activity is no longer active 
     if (mGoogleApiClient != null) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     } 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) 
    { 
     mGoogleMap=googleMap; 
     mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 

     //Initialize Google Play Services 
     if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      if (ContextCompat.checkSelfPermission(this, 
        Manifest.permission.ACCESS_FINE_LOCATION) 
        == PackageManager.PERMISSION_GRANTED) { 
       //Location Permission already granted 
       buildGoogleApiClient(); 
       mGoogleMap.setMyLocationEnabled(true); 
      } else { 
       //Request Location Permission 
       checkLocationPermission(); 
      } 
     } 
     else { 
      buildGoogleApiClient(); 
      mGoogleMap.setMyLocationEnabled(true); 
     } 
    } 

    protected synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(1000); 
     mLocationRequest.setFastestInterval(1000); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     } 
    } 

    @Override 
    public void onLocationChanged(Location location) 
    { 
     mLastLocation = location; 
     if (mCurrLocationMarker != null) { 
      mCurrLocationMarker.remove(); 
     } 


     LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
     MarkerOptions markerOptions = new MarkerOptions(); 
     markerOptions.position(latLng); 
     markerOptions.title("Current Position"); 
     markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)); 
     markerOptions.anchor(.5f,.95f); 
     mCurrLocationMarker = mGoogleMap.addMarker(markerOptions); 

     //move map camera 
     mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,12f)); 


     if (mGoogleApiClient != null) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     } 

    } 

    private void fetchServiceProviders() 
    { 
     //volley call and populate in list 
    } 



} 
+0

@ Dhruv for-Schleife ein verwenden Tyagi, was ist dann dein Problem? –

Antwort

3

Sie einfach diese Methode verwenden können:

@Override 
    public void onMapReady(GoogleMap map) { 
     for(ServiceProviderItem item : workshopList){ 
     map.addMarker(new MarkerOptions() 
      .position(new LatLng(item.lat, item.lng)) 
      .title("Hello world")); 
     } 
    } 
+0

Ich habe einen Fehler LatLng (double, double) in LatLng kann nicht angewendet werden (java.lang.string, java.lang.string) ... so konvertiere ich sring zu double –

+0

@DhruvTyagi das ist toll –

+0

bt immer noch Problem Gibt es –

1

versuchen Sie dies:

private GoogleMap googleMap; 
private MarkerOptions options = new MarkerOptions(); 
private ArrayList<LatLng> latlngs = new ArrayList<>(); //define arraylist of latlng 

Sie lat lange auf die Arraylist Verwendung hinzufügen

latlngs.add(new LatLng(21.334343, 92.43434)); 

nun für sie auf der Karte zeigen

for (LatLng point : latlngs) { 
    options.position(point); 
    options.title("Marker Title"); 
    options.snippet("Marker Desc"); 
    googleMap.addMarker(options); 
} 
Verwandte Themen