gpt4 book ai didi

java - 在谷歌地图 Android 上显示当前位置

转载 作者:行者123 更新时间:2023-12-01 10:47:48 25 4
gpt4 key购买 nike

我知道有很多类似的问题,我读过其中很多,但没有一个对我有用。部分原因是谷歌地图 sdk 发生了变化。

我想要做的是向用户显示他在谷歌地图上的当前位置。

这是我所做的:

在 gradle 文件中的依赖项中添加了以下内容:

compile 'com.google.android.gms:play-services:7.5.0'

在 list 文件中添加了以下内容:

<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

....


<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />

我的 fragment 的onCreateView:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mapFragment = (com.google.android.gms.maps.MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
if (mapFragment == null){
fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mapFragment = com.google.android.gms.maps.MapFragment.newInstance();
fragmentTransaction.replace(R.id.map, mapFragment).commit();
}

if (mapFragment != null){
initilizeMap();
}

// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_map, container, false);
}

initializeMap方法:

private void initilizeMap(){
googleMap = mapFragment.getMap();

if (googleMap != null){
googleMap.getUiSettings().setAllGesturesEnabled(true);
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);

googleMap.setOnMyLocationChangeListener(myLocationChangeListener());
}
}

这是应该为我提供当前位置的 locationChangeListener:

private GoogleMap.OnMyLocationChangeListener myLocationChangeListener() {
return new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());

CameraPosition cameraPosition = new CameraPosition.Builder().target(loc).zoom(15.0f).build();

CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
googleMap.moveCamera(cameraUpdate);
}
}

发生的情况是 map 出现但不显示当前位置。它展示了整个世界。

我错过了什么或做错了什么?提前致谢。

最佳答案

首先,您必须设置 Google Api 客户端,以获取当前位置。您可以在此处查看如何设置 GoogleApiClient:

public class MainActivity extends AppCompatActivity  implements `GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, LocationListener {`
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createLocationRequest();
TextView mTextView = (TextView)findViewById(R.id.mTextView);
mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buildGoogleApipClient();
mGoogleApiClient.connect();


}
});

}

protected synchronized void buildGoogleApipClient() {
mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
protected void onResume() {
super.onResume();
Toast.makeText(this, "Yes calling", Toast.LENGTH_LONG).show();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onConnected(Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if(mLastLocation!=null){
Toast.makeText(this,mLastLocation.getLatitude()+"-----"+mLastLocation.getLongitude(),Toast.LENGTH_LONG).show();
}
if (true) {
startLocationUpdates();
}


}

关于java - 在谷歌地图 Android 上显示当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34075534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com