2017-06-04 64 views
0

我使用Mapbox與Geofire/Geoquery一起解僱了我的當前用戶職位。我想在Mapbox地圖上放置標記以實時顯示我的用戶。我使用:使用geofire,mapbox在Android上實時顯示多個職位

// object or in the same activity which contains the mapview. 
Mapbox.getInstance(this, getString(R.string.access_token)); 

mapView = (MapView) findViewById(R.id.mapView); 
mapView.onCreate(savedInstanceState); 
mapView.getMapAsync(new OnMapReadyCallback() { 
    @Override 
    public void onMapReady(final MapboxMap mapboxMap) { 
     map = mapboxMap; 

     GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(latitude, longitude), 100); 

     geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() { 
      @Override 
      public void onKeyEntered(String key, GeoLocation location) { 
       Log.d("test","Key "+key+" entered the search area at ["+location.latitude+","+location.longitude+"]"); 
       mapboxMap.addMarker(new MarkerOptions().position(new LatLng(location.latitude, location.longitude))); 
      } 

      @Override 
      public void onKeyExited(String key) { 
       Log.d("test","Key %s is no longer in the search area"+key); 
      } 

      @Override 
      public void onKeyMoved(String key, GeoLocation location) { 
       Log.d("test", "Key %s moved within the search area to [%f,%f]" + key + location.latitude + location.longitude); 
      } 

      @Override 
      public void onGeoQueryReady() { 
       Log.d("test","All initial data has been loaded and events have been fired!"); 
      } 

      @Override 
      public void onGeoQueryError(DatabaseError error) { 
       Log.d("test","There was an error with this query: " + error); 
      } 
     }); 


    } 
}); 

這是工作,但只有當我開始我的活動,我現在正在使用的SFVehicles例如創建現場查詢第一次:

private GeoFire geoFire; 
private GeoQuery geoQuery; 
private MapView mapView; 
private MapboxMap map; 
private Map<String,Marker> markers; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_rallye_main); 

    // setup map and camera position 
    SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 

    this.map = mapFragment.getMap(); 

    LatLng latLngCenter = new LatLng(INITIAL_CENTER.latitude, INITIAL_CENTER.longitude); 
    this.map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLngCenter, INITIAL_ZOOM_LEVEL)); 
    this.map.setOnCameraChangeListener(this); 

    FirebaseOptions options = new FirebaseOptions.Builder().setApplicationId("geofire").build(); 
    FirebaseApp app = FirebaseApp.initializeApp(this, options); 

    // setup GeoFire 
    this.geoFire = new GeoFire(FirebaseDatabase.getInstance(app).getReferenceFromUrl(GEO_FIRE_REF)); 
    // radius in km gibt alle positionen der Teilnehmer im radius von x km aus 
    this.geoQuery = this.geoFire.queryAtLocation(INITIAL_CENTER, 50); 

    // setup markers 
    this.markers = new HashMap<String, Marker>(); 

} 

@Override 
protected void onStop() { 
    super.onStop(); 
    // remove all event listeners to stop updating in the background 
    this.geoQuery.removeAllListeners(); 
    for (Marker marker: this.markers.values()) { 
     marker.remove(); 
    } 
    this.markers.clear(); 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    // add an event listener to start updating locations again 
    this.geoQuery.addGeoQueryEventListener(this); 
} 

@Override 
public void onKeyEntered(String key, GeoLocation location) { 
    // Add a new marker to the map 
    Marker marker = this.map.addMarker(new MarkerOptions().position(new LatLng(location.latitude, location.longitude))); 
    this.markers.put(key, marker); 
} 

@Override 
public void onKeyExited(String key) { 
    // Remove any old marker 
    Marker marker = this.markers.get(key); 
    if (marker != null) { 
     marker.remove(); 
     this.markers.remove(key); 
    } 
} 

@Override 
public void onKeyMoved(String key, GeoLocation location) { 
    // Move the marker 
    Marker marker = this.markers.get(key); 
    if (marker != null) { 
     this.animateMarkerTo(marker, location.latitude, location.longitude); 
    } 
} 

@Override 
public void onGeoQueryReady() { 
} 

@Override 
public void onGeoQueryError(DatabaseError error) { 
    new AlertDialog.Builder(this) 
      .setTitle("Error") 
      .setMessage("There was an unexpected error querying GeoFire: " + error.getMessage()) 
      .setPositiveButton(android.R.string.ok, null) 
      .setIcon(android.R.drawable.ic_dialog_alert) 
      .show(); 
} 

// Animation handler for old APIs without animation support 
private void animateMarkerTo(final Marker marker, final double lat, final double lng) { 
    final Handler handler = new Handler(); 
    final long start = SystemClock.uptimeMillis(); 
    final long DURATION_MS = 3000; 
    final Interpolator interpolator = new AccelerateDecelerateInterpolator(); 
    final LatLng startPosition = marker.getPosition(); 
    handler.post(new Runnable() { 
     @Override 
     public void run() { 
      float elapsed = SystemClock.uptimeMillis() - start; 
      float t = elapsed/DURATION_MS; 
      float v = interpolator.getInterpolation(t); 

      //double currentLat = (lat - startPosition.latitude) * v + startPosition.latitude; 
      //double currentLng = (lng - startPosition.longitude) * v + startPosition.longitude; 
      double currentLat = (lat - startPosition.getLatitude()) * v + startPosition.getLatitude(); 
      double currentLng = (lng - startPosition.getLongitude()) * v + startPosition.getLongitude(); 
      marker.setPosition(new LatLng(currentLat, currentLng)); 

      // if animation is not finished yet, repeat 
      if (t < 1) { 
       handler.postDelayed(this, 16); 
      } 
     } 
    }); 
} 

private double zoomLevelToRadius(double zoomLevel) { 
    // Approximation to fit circle into view 
    return 16384000/Math.pow(2, zoomLevel); 
} 


@Override 
public void onCameraChange(CameraPosition cameraPosition) { 
    // Update the search criteria for this geoQuery and the circle on the map 
    LatLng center = cameraPosition.target; 
    double radius = zoomLevelToRadius(cameraPosition.zoom); 
    this.geoQuery.setCenter(new GeoLocation(center.getLatitude(), center.getLongitude())); 
    // radius in km 
    this.geoQuery.setRadius(radius/1000); 
} 

的示例代碼使用谷歌地圖,但我不得不使用Mapbox提供離線地圖,我就在這行錯誤:

SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 

this.map = mapFragment.getMap(); 

具體做法是:

Cannot resolve Method getMap() 

我已經嘗試過這樣的:

mapFragment.getMapAsync(new OnMapReadyCallback() { 
    @Override 
    public void onMapReady(MapboxMap mapboxMap) { 

     // Customize map with markers, polylines, etc. 

    } 
}); 

但沒有什麼,我得到這個錯誤:

FATAL EXCEPTION: main Process: com.test.bla, PID: 30149 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.bla/com.test.bla.rallye.rallye_main}: android.view.InflateException: Binary XML file line >#2: Error inflating class fragment 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2689) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2754) 
at android.app.ActivityThread.access$900(ActivityThread.java:177) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5938) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:483) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
at android.transition.Scene.enter(Scene.java:175) 
at com.android.internal.policy.impl.PhoneWindow.transitionTo(PhoneWindow.java:483) 
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:426) 
at android.app.Activity.setContentView(Activity.java:2267)..... 

我無法從谷歌的MapX轉換這個例子Mapbox

佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:mapbox="http://schemas.android.com/apk/res-auto" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

    <com.mapbox.mapboxsdk.maps.MapView 
     android:id="@+id/mapView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     mapbox:mapbox_cameraTargetLat="52.00000" 
     mapbox:mapbox_cameraTargetLng="8.00000" 
     mapbox:mapbox_styleUrl="@string/mapbox_style_mapbox_streets" 
     mapbox:mapbox_cameraZoom="12"/> 

</RelativeLayout> 

或我的第二次嘗試:

<?xml version="1.0" encoding="utf-8"?> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

回答

0

它看起來像你的佈局xml中的東西是不正確的。您是否想要誇大谷歌地圖或地圖盒地圖?請包含佈局XML。

+0

嗨,是使用mapbox MapView。編輯firstpost – georf

相關問題