如何更改GoogleMap V2相机中心? [英] How to change GoogleMap V2 camera center?
问题描述
我正在尝试将Google map V2相机的中心位置更改为低于地图视图的中心.
到目前为止,借助一些解决方案(例如
我想要的是相机位置将在我的目标位置.
Google地图导航:
您应使用
是将 MapFragment
( MapView
)的高度缩放到 dHeight
,然后是可见区域(屏幕高度),其中:
dHeight =(int)(k * MapFragment.height)
其中 k = Target_pos.y/cam_pos.y
.在这种情况下,地图中心会精确地移动到您想要的点(但是底部(图片上的白色)部分将不可见).另外,您还应该向上移动缩放控件,因为默认情况下,缩放控件也位于地图底部,因此也不可见.您可以在此答案中进行操作.但是请记住,根据 Google Maps API服务条款,您的应用程序不得删除或遮盖Google徽标或版权声明.
似乎正确的解决方案是创建自定义视图,该视图扩展了 MapView
,例如就像在 中回答并做所有的每个位置偏移都会计算出其中的魔法.
I am trying to change Google map V2 camera center position to be lower to the center of the map view.
So far, I've partially succeeded to achive this with the help of a few solutions such as this
This solution did the job but the actual camera position still remains in the center of the map! It "pushed" the target location down but the camera still "looking" on the center of the MapView.
The problem with this approach comes when you want do animations and map movement such in navigation.
You need to calculate the offset for every location you get from the GPS and when you do hard turns the offset is not the same as Camera animation route is not in my control .
For instance, if I move tward some direction on foot and then suddenly I turn 180 degrees, the animation will push my target location until the animation complete and then it will look ok, thats because the the map rotation and animation is always around the cam position which is always in the center of the view.
Fo instance, in Google Map Navigation - The Nav marker is at the bottom of the map, and it seems to rotate and animate around that position and not around the view center.
Here is a screen shot the result i've got from above solutions:
What I want is that the camera position will be in my target position.
Google map navigation:
You should use Map Padding, namely the setPadding(int left, int top, int right, int bottom)
method:
...
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.setPadding(0, dHeight, 0, 0);
...
}
...
where dHeight = Target_pos.y - cam_pos.y
. And you should move compass button, like in this answer of Vignon with some changes for drawing compass outside of clipping region:
try {
final ViewGroup parent = (ViewGroup) mMapFragment.getView().findViewWithTag("GoogleMapMyLocationButton").getParent();
parent.post(new Runnable() {
@Override
public void run() {
try {
Resources r = getResources();
//convert our dp margin into pixels
int marginPixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
// Get the map compass view
View mapCompass = parent.getChildAt(4);
View v = mapCompass;
while (v.getParent() != null && v.getParent() instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) v.getParent();
viewGroup.setClipChildren(false);
viewGroup.setClipToPadding(false);
v = viewGroup;
}
// create layoutParams, giving it our wanted width and height(important, by default the width is "match parent")
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(mapCompass.getHeight(),mapCompass.getHeight());
// position on top right
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
//give compass margin
rlp.setMargins(marginPixels, dHeight, marginPixels, marginPixels);
mapCompass.setLayoutParams(rlp);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
Also you can try that dirty solution (see the picture)
is to scale height of MapFragment
(MapView
) to dHeight
more then visible area (screen height), where:
dHeight = (int) (k * MapFragment.height)
where k = Target_pos.y / cam_pos.y
. In that case map center moved exactly to your desired point (but bottom (white on picture) part of map will not be visible). Also you should move up zoom controls, because by default it also at the bottom of map and will not be visible too. You can do this like in this answer. But remember, that as per the Google Maps APIs Terms of Service, your application must not remove or obscure the Google logo or copyright notices.
Seems the right solution is to create custom view, which extends MapFragment
or MapView
, e.g. like in that answer and do all your every location offset calculate magic inside it.
这篇关于如何更改GoogleMap V2相机中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!