我有谷歌Map设置与我的应用程序,可以查看Map与一个蓝色的点表示我的位置在Map上,但我试图中心和放大的位置自动似乎没有工作。
当我按下右上角的目标图标时,相机视图会自动放大到我的位置。
我尝试了以下方法:
LatLng latlngPos = new LatLng(location.getLatitude(), location.getLongitude())
myMap.moveCamera(CameraUpdateFactory.newLatLng(latlngPos));
myMap.animateCamera(CameraUpdateFactory.zoomTo(15));
也试过了
myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlngPos, 15));
但似乎两者都不起作用。我在onlocation中更改了这些代码行,如我遵循的教程所示。我尝试了不同的建议,从类似的问题在这里也和它仍然似乎被缩小,但突出我目前的位置在正确的位置上的一个蓝色的点在Map上。
我错过了什么明显的东西吗?谢谢
编辑:整个activity:-
public class FindBankActivity extends Activity implements LocationListener {
private GoogleMap myMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Make sure Google Play Services available
if(isGooglePlay()){
setContentView(R.layout.activity_find_bank);
setUpMap();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.find_bank, menu);
return true;
}
public boolean isGooglePlay() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(status == ConnectionResult.SUCCESS) {
return(true);
}
else
{
((Dialog) GooglePlayServicesUtil.getErrorDialog(status, this, 15)).show();
//Toast.makeText(this, "Google Play is not available", Toast.LENGTH_SHORT).show();
}
return(false);
}
public void setUpMap() {
if(myMap == null) {
myMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
if(myMap != null) {
//Initiate map
myMap.setMyLocationEnabled(true);
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
String provider = lm.getBestProvider(new Criteria(), true);
if(provider == null) {
onProviderDisabled(provider);
}
Location loc = lm.getLastKnownLocation(provider);
if(loc == null) {
LocationListener locListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng ll = new LatLng(lat,lng);
myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 15));
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider,
int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
lm.requestLocationUpdates(provider, 20000, 0, locListener);
}
if(loc != null) {
onLocationChanged(loc);
}
}
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
public void onLocationChanged(Location location) {
//Get the location
LatLng latlngPos = new LatLng(location.getLatitude(), location.getLongitude());
//Display on the map and zoom in
// myMap.moveCamera(CameraUpdateFactory.newLatLng(latlngPos));
// myMap.animateCamera(CameraUpdateFactory.zoomTo(15));
myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlngPos, 15));
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
2条答案
按热度按时间2o7dmzc51#
你学错了教程。如果您想获得蓝点位置更改的更新,请使用
OnMyLocationChangeListener
用你的GoogleMap
对象而不是LocationManager
.这些方法可以返回不同的结果,因为
GoogleMap
使用融合的位置提供程序和LocationManager
仅原始gps或网络提供商。如果你担心
OnMyLocationChangeListener
不赞成,请看我的独白。3b6akqbq2#
我猜你忘了添加Map用户界面设置