ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android如何关注当前位置

2019-07-25 12:23:51  阅读:154  来源: 互联网

标签:android google-maps location


嗨,我有一个Android应用程序,在谷歌地图上找到你的位置,但当我启动应用程序时,它从非洲开始,而不是在我目前的城市,国家,地点等.我已经在developer.android.com上检查了信息与位置问题有关,但问题仍然存在.

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

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

}


 }

解决方法:

您也可以在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 maps扩展.伟大的图书馆BTW.我真的很喜欢这种方法,因为它立即加载它,你永远不会看到非洲. IMO似乎是最好的.

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

标签:android,google-maps,location
来源: https://codeday.me/bug/20190725/1533037.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有