2013-09-21 66 views
3

嗨我有一個Android應用程序,可以在谷歌地圖上找到你的位置,但是當我開始從非洲開始的應用程序不在我目前的城市,國家,地點等。我已經檢查了與位置問題有關的developer.android.com上的信息,但問題仍然存在。Android如何專注於當前位置

這是代碼;有任何想法嗎?感謝..

package com.kodlab.nerdeyim; 

    import android.location.Location; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.support.v4.app.FragmentActivity; 

    import com.google.android.gms.common.ConnectionResult; 
    import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks; 
    import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener; 
    import com.google.android.gms.location.LocationClient; 
    import com.google.android.gms.location.LocationListener; 
    import com.google.android.gms.location.LocationRequest; 
    import com.google.android.gms.maps.CameraUpdateFactory; 
    import com.google.android.gms.maps.GoogleMap; 
    import com.google.android.gms.maps.SupportMapFragment; 
    import com.google.android.gms.maps.model.LatLng; 

    public class MainActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener { 

private LocationClient locationClient; 
private LocationRequest locationRequest; 
private GoogleMap googleMap; 

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

    locationClient = new LocationClient(this, this, this); 

    locationRequest = LocationRequest.create() 
       .setInterval(5000) 
       .setFastestInterval(500) 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

    SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
    googleMap = supportMapFragment.getMap(); 
    googleMap.setMyLocationEnabled(true); 
} 

@Override 
public void onLocationChanged(Location location) { 
    HaritadaKonumGosterAsyncTask task = new HaritadaKonumGosterAsyncTask(); 
    task.execute(new Location[] {location}); 
} 

@Override 
public void onConnected(Bundle connectionHint) { 
    locationClient.requestLocationUpdates(locationRequest, this); 
} 

@Override 
public void onDisconnected() {} 

@Override 
public void onConnectionFailed(ConnectionResult result) {} 

@Override 
protected void onResume() { 
    super.onResume(); 
    locationClient.connect(); 
} 

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

    if(locationClient.isConnected()) 
     locationClient.removeLocationUpdates(this); 

    locationClient.disconnect(); 
} 

private class HaritadaKonumGosterAsyncTask extends AsyncTask<Location, Void, LatLng> { 

    @Override 
    protected LatLng doInBackground(Location... params) { 
     Location konum = params[0]; 
     return new LatLng(konum.getLatitude(), konum.getLongitude()); 
    } 

    @Override 
    protected void onPostExecute(LatLng konum) { 
     googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(konum, 15)); 
    } 

} 


} 
+0

你在ASycn里弄錯Latlng – Metalhead1247

+0

所以怎麼回事? – regeme

+0

這就是Google地圖所做的。它從非洲開始。請參閱下面的答案。 – jasonflaherty

回答

3

您也可以在您的XML佈局中設置它(設置北美洲的中間部分):

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:map="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/map" 
    android:name="pl.mg6.android.maps.extensions.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    map:cameraBearing="0" 
    map:cameraTargetLat="53.938305" 
    map:cameraTargetLng="-112.763672" 
    map:cameraTilt="30" 
    map:cameraZoom="2" /> 

我在該示例中使用了android地圖擴展。偉大的圖書館BTW。我很喜歡這種方法,因爲它馬上加載它,你永遠不會看到非洲。海事組織似乎是最好的。

注意!我應該警告你,我得到一個編譯錯誤,直到我創建一個空間,然後在我完成一天後啓動Eclipse時再次保存文件...任何人都知道這是爲什麼?

+0

我不明白爲什麼這是被接受的答案,甚至爲什麼它有3個upvotes。不要誤解@jasonflaherty,你的文章有點幫助,但它不回答這個問題(要求指出當前位置,而不是靜態位置) –

+0

嗨,Cliff,問題如何開始在非洲以外的其他地方,我是否認爲他在美國?我明白你在說什麼。人們可以先找到他們的位置,然後加載地圖。 – jasonflaherty

0

你不需要爲這個AsycTask刪除它,然後在onlocationchanged()方法盡一切本身

 public void onLocationChanged(Location location) 
     { 
      googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location, 15)); 
      } 
3

感謝您的代碼解決我的問題,現在我幫

public void onLocationChanged(Location location) 
{ 
    // Getting latitude of the current location 
    double latitude = location.getLatitude(); 

    // Getting longitude of the current location 
    double longitude = location.getLongitude(); 

    float speed = location.getSpeed(); 

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

    // Showing the current location in Google Map 
    CameraPosition camPos = new CameraPosition.Builder() 
    .target(new LatLng(latitude, longitude)) 
    .zoom(18)   
    .bearing(location.getBearing()) 
    .tilt(70) 
    .build(); 
    CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(camPos); 
    googleMap.animateCamera(camUpd3); 
}