Android如何关注当前位置 [英] Android How to focus on current position

查看:112
本文介绍了Android如何关注当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个Android应用程序可以在Google地图上找到您的位置,但是当我启动应用程序时,它从非洲开始,而不是在当前的城市,国家/地区等。我已经查看了有关开发人员的信息。 android.com与位置问题相关,但问题仍然存在。



以下是代码;有任何想法吗?谢谢..

  package com.kodlab.nerdeyim; 

导入android.location.Location;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入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;
导入com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

公共类MainActivity扩展FragmentActivity实现ConnectionCallbacks,OnConnectionFailedListener,LocationListener {

private LocationClient locationClient;
private LocationRequest locationRequest;
私人GoogleMap google地图;

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

locationClient =新的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(新位置[] {位置});
}

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

$ b @Override
public void onDisconnected(){}
$ b @Override
public void onConnectionFailed(ConnectionResult result){}

@Override
保护无效的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> {

@覆盖
保护LatLng doInBackground(Location ... params){
位置konum = params [0];
返回新的LatLng(konum.getLatitude(),konum.getLongitude());
}

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

}


}


<您也可以在您的XML布局中设置它(设置北美的中间英语):

<$ p $

p> < 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 maps扩展。伟大的图书馆BTW。我很喜欢这种方法,因为它马上加载它,你永远不会看到非洲。海事组织似乎是最好的。



注意!我应该警告你,我得到一个编译错误,直到我创建一个空间,然后在我完成一天后启动Eclipse时再次保存文件...任何人都知道这是为什么?


Hi I've an android app that finds your location on google maps, but when I've started the app it started from Africa not in my current city,country,location etc. I've already checked info's on developer.android.com related with location issues but the problem persists.

Here is the code; any ideas? thanks..

  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));
    }

}


 }

解决方案

You can set it in your XML layout also (sets the middlish of North America):

<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" />

I'm using the android maps extension in that example. Great library BTW. I really like this approach as it loads it right away and you never see Africa. IMO it seems to be the best.

Note! I should warn you that I get a compile error until I make a space and then save the file again when I start Eclipse up after I'm done for the day... Anyone know why this is?

这篇关于Android如何关注当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆