2013-08-27 85 views
0

這是當前的代碼,我必須在Google地圖上顯示上次已知的良好位置作爲標記。所有功能,但我想更新的時間間隔(例如1分鐘或5分鐘)。什麼是最好的方式來實現這給我當前使用的代碼?更新間隔位置

import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.maps.CameraUpdate; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
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; 

public class Parking extends Activity implements OnClickListener { 

    private GoogleMap map; 

    Button b; 

    public static final String LATITUDE = "latitude"; 
    public static final String LONGITUDE = "longitude"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.parking); 

     b = (Button)findViewById(R.id.button1); 
     b.setOnClickListener(this); 

     map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

     if (map!=null) 
     {  

     //Set Map Type 
     map.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 

     //Enable MyLocation Layer of Google Map 
     map.setMyLocationEnabled(true); 

     //Get LocationManager object from System Service LOCATION_SERVICE 
     LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

     //Create a criteria object to retrieve provider 
     Criteria criteria = new Criteria(); 

     //Get the best provider 
     String provider = locationManager.getBestProvider(criteria, true); 

     //Get Current Location 
     Location myLocation = locationManager.getLastKnownLocation(provider); 

     //Get Lat/Long of the current location 
     double latitude = myLocation.getLatitude(); 
     double longitude = myLocation.getLongitude(); 

     //Create a latLng object for the current location 
     LatLng latLng = new LatLng(latitude, longitude); 

     //Show the current location in Google Map 
     map.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

     //Zoom to location 
     map.animateCamera(CameraUpdateFactory.zoomTo(16)); 

     //Add marker 
     map.addMarker(new MarkerOptions() 
      .position(new LatLng(latitude, longitude)) 
      .title("You Are Here") 
     ); 




     }//end if map !null 
    } 
+0

您是否試過Fused Locations提供程序? –

回答

1

可以使用的LocationManager類的requestLocationUpdates方法 - http://developer.android.com/reference/android/location/LocationManager.html

您可以指定以毫秒計的時間間隔,也以米爲單位的最小距離變化。

我的建議是按如下說明切換到最新的位置API:https://developer.android.com/google/play-services/location.html

+0

作爲一個新人,花了我一段時間才弄明白這一點,但事實證明它並不是那麼複雜。由於在保存我想要的新信息之前,我已經有了一個按鈕,所以我只實現了這個方法並將LocationListener擴展到Activity。再次感謝。 –

0

與谷歌新推出了融合位置提供它得到的位置或獲得定期更新要容易得多!請參閱此鏈接可能是非常有幫助的

https://developer.android.com/training/location/receive-location-updates.html

所有你需要的是太導入谷歌播放服務到你的項目,

,你也可以使用一個後臺服務得到定期更新。

public class UpdateLocation extends Service 
     implements 
      GooglePlayServicesClient.ConnectionCallbacks, 
      GooglePlayServicesClient.OnConnectionFailedListener, 
      LocationListener { 

private static final long UPDATE_INTERVAL = 300000; 
private static final int FASTEST_INTERVAL = 60000; 
private LocationRequest lr; 
private LocationClient lc; 
private Location l; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    lr = LocationRequest.create(); 
    lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    lr.setInterval(UPDATE_INTERVAL); 
    lr.setFastestInterval(FASTEST_INTERVAL); 
    lc = new LocationClient(this, this, this); 
} 


@Override 
public void onStart(Intent intent, int startId) { 
    lc.connect(); 

} 

@Override 
public void onLocationChanged(Location l) { 
//do updates on Location change. 
} 

@Override 
public void onConnectionFailed(ConnectionResult arg0) { 

} 

@Override 
public void onConnected(Bundle arg0) { 
    Log.d("AutoTracker : Test", "Location Client Connected"); 
    if (updates) { 
     lc.requestLocationUpdates(lr, this); 
    } 

} 

@Override 
public void onDestroy() { 
    if (lc.isConnected()) { 
     lc.removeLocationUpdates(this); 
    } 
    lc.disconnect(); 
} 

@Override 
public void onDisconnected() { 
} 

@Override 
public IBinder onBind(Intent arg0) { 
    return null; 
} 

} 
+0

這對我來說最合乎邏輯。我如何將這個實現到上面的代碼中?我會用這個班的某個電話來替換最後一個已知的好位置嗎?我將如何檢索lat/Lng? –

+0

http://hastebin.com/fomequyawe.avrasm,試試這個代碼,由你的代碼組成,如果有幫助,請接受答案! –

+0

感謝您的幫助,但我收到了您提供的代碼的幾處錯誤,儘管這對我創建解決方案有一定幫助。 –