2016-06-12 6 views
-1

In meiner Android-App muss ich die aktuelle GPS/Netzwerk-Position und dann die Google-Karte dieser Position bekommen.Aber bevor Sie die Position, die Karte wurde geladen und jeder Zeit es bekommt (0,0) Position. Wie kann ich den On mapready Anruf verzögern, bis ich Position bekomme.So verzögern onMapready Anruf in Google Karte basierte App

Code

package com.example.partha.map; 

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.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 
    private Position position; 

    @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) { 
     try { 
      mMap = googleMap; 

      // Add a marker in Sydney and move the camera 

      position = new Position(this); 
      position.getLocation(Position.NETWORK); 
      LatLng sydney = new LatLng(position.getLatitude(),position.getLongitude()); 

      Log.d("position",position.getLatitude()+" "+position.getLatitude()); 
      mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in your position")); 
      Log.d("Added marker", " at position"); 
      mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
      Log.d("moved marker", " at position"); 
     }catch (Exception e) 
     { 
      Log.d("from on map",e.getMessage()); 
     } 
    } 
} 

position.class

public class Position { 
     private boolean GPSPosition; 
     private boolean networkPosition; 
     private boolean SavedPosition; 
     private Context context; 
     private LatLng latLng; 
     private double latitude; 
     private double longitude; 
     private LocationManager locationManager; 
     private LocationListener listener; 
     static public final int GPS = 2; 
     static public final int NETWORK = 1; 
     private float minDistance=5000; 
     private long updateTime=0; 


     public Position(Context context) { 
      this.context = context; 
      locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
      setListener(); 

     } 

     public Position(Context context, float minDistance, long updateTime) { 
      this.context = context; 
      this.minDistance = minDistance; 
      this.updateTime = updateTime; 
     } 

     public void setUpdateTime(long updateTime) { 
      this.updateTime = updateTime; 
     } 

     public void setMinDistance(float minDistance) { 
      this.minDistance = minDistance; 
     } 

     @TargetApi(Build.VERSION_CODES.M) 
     public void getLocation(int source) { 
      if (source == GPS) { 
       if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
        // TODO: Consider calling 
        // ActivityCompat#requestPermissions 
        // here to request the missing permissions, and then overriding 

        return; 
       } 
       else 
       { 
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, updateTime, minDistance, listener); 
       } 
      } 
      else if(source==NETWORK) 
      { 

       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,updateTime,minDistance,listener); 
      } 
     } 

     public double getLongitude() { 
      return longitude; 
     } 

     public double getLatitude() { 
      return latitude; 
     } 

     public LatLng getLatLng() { 
      return latLng; 
     } 

     private void setListener() 
     { 
      listener=new LocationListener() { 
       @Override 
       public void onLocationChanged(Location location) { 

        latitude=location.getLatitude(); 
        longitude=location.getLongitude(); 
        latLng=new LatLng(latitude,longitude); 
        Log.d("Your position",latitude+" "+longitude); 

       } 

       @Override 
       public void onStatusChanged(String provider, int status, Bundle extras) { 

       } 

       @Override 
       public void onProviderEnabled(String provider) { 

       } 

       @Override 
       public void onProviderDisabled(String provider) { 
        Intent settingsActivity = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        context.startActivity(settingsActivity); 

       } 
      }; 
     } 
    } 

Antwort

0

Try this: get latlng vor getMapAsync aufrufen. Entfernen Sie den Einstieg in die onMapReady-Methode. eine Schnittstelle in Position Klasse wie

public interface LocationListener{ 
void onSuccessLocation(Position position); 
void onFailLocation(); 
} 

und implementieren sie in Ihrer Tätigkeit

LatLng sydney; 
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); 


    position = new Position(this); 
      position.setListener(); 
      position.getLocation(Position.NETWORK); 
      sydney = new LatLng(position.getLatitude(),position.getLongitude()); 

     mapFragment.getMapAsync(this); 
    } 

Ein anderer Weg, erstellen. Rufen Sie diese Methode auf, wenn der Speicherort erfolgreich ist oder fehlgeschlagen ist. Und onSuccessLocation() ruft getMapAsync auf.

In Position Klasse OnLocationChanged Methodenaufruf onSuccessLocation

public void onLocationChanged(Location location) { 

        latitude=location.getLatitude(); 
        longitude=location.getLongitude(); 
        latLng=new LatLng(latitude,longitude); 
        Log.d("Your position",latitude+" "+longitude); 
        if(context != null){ 
        ((LocationListener)context).onSuccessLocation(Position.this); 
} 
       }