2013-09-21 65 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); 
}