如何在Android的Google Map中聚焦于Marker [英] How to Focus at Marker in google map in android

查看:69
本文介绍了如何在Android的Google Map中聚焦于Marker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想知道我们是否可以专注于android应用程序中添加的标记.如果是,怎么办?还是有其他替代方法可以完成此任务.

I just want to know whether we can focus at added marker in android application or not. If yes, how? or is there any alternative way to get this task done.

让我说我使用下面的代码添加了一个标记:

lets say I have added a marker using below code :

map.addMarker(new MarkerOptions()
            .title(title)
            .snippet(snippet)
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
            .position(pos)
                );

如何以最大变焦聚焦在此标记上.如果我再添加1个标记,则应该以这样的方式调整缩放比例(最大可能的缩放比例),以使两个标记都立即显示.
这是我正在尝试执行的操作,但是在行map.moveCamera(cu);上失败.

how to focus at this marker with maximum zoom. and if I will add 1 more marker it should adjust zooming(maximum possible zoom) in such a way so that both marker will display at once.
This is what I am trying to do but it is failing at line map.moveCamera(cu);.

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.CancelableCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class LocateUserInMap extends FragmentActivity {
    private GoogleMap map;
    private List<Marker> markers;

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

        markers = new ArrayList<Marker>();
        Intent intent = getIntent();
        double frndLatVal = intent.getDoubleExtra("frndLatVal", 0);
        double frndLonVal = intent.getDoubleExtra("frndLonVal", 0);
        LatLng myloc = new LatLng(19.115486500000000000, 72.905544299999970000);
        LatLng friendloc = new LatLng(frndLatVal, frndLonVal);
        map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.google_map)).getMap();
        addToMap(myloc, "My location", "From here 0M", "blue");
        addToMap(friendloc, "Friend location", "From here 100M", "red");
        showMarkersAtOnce();
    }

    private void showMarkersAtOnce() {
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for (Marker m : markers) {
            builder.include(m.getPosition());
        }
        LatLngBounds bounds = builder.build();
        int padding = 0; // offset from edges of the map in pixels
        CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
        map.moveCamera(cu);
    /*  map.getUiSettings().setScrollGesturesEnabled(false);
        map.animateCamera(cu,
                //CameraUpdateFactory.newLatLng(new LatLng(lat, lng))
                 new CancelableCallback()
        {

            @Override
            public void onFinish()
            {
                map.getUiSettings().setScrollGesturesEnabled(true);

            }

            @Override
            public void onCancel()
            {
                map.getUiSettings().setAllGesturesEnabled(true);

            }
        });*/
      //  map.animateCamera(CameraUpdateFactory.zoomBy(13));
    }

    private void addToMap(LatLng pos, String title, String snippet,
            String markercolor) {
        Marker localmarker = null;
        if (markercolor == "blue")
        {
            localmarker = map.addMarker(new MarkerOptions()
                    .title(title)
                    .snippet(snippet)
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                    .position(pos));
        } else if (markercolor == "red") 
        {
            localmarker = map.addMarker(new MarkerOptions()
                    .title(title)
                    .snippet(snippet)
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_RED))
                    .position(pos));
        }

        markers.add(localmarker);
    }

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

}

和Logcat

08-04 13:23:32.975: W/EGL_emulation(764): eglSurfaceAttrib not implemented
08-04 13:23:42.787: D/dalvikvm(764): GC_CONCURRENT freed 169K, 3% free 11335K/11591K, paused 130ms+40ms, total 336ms
08-04 13:23:49.812: D/dalvikvm(764): GC_CONCURRENT freed 273K, 3% free 11497K/11847K, paused 121ms+124ms, total 480ms
08-04 13:23:49.972: E/ActivityThread(764): Failed to find provider info for com.google.settings
08-04 13:23:50.259: E/ActivityThread(764): Failed to find provider info for com.google.settings
08-04 13:23:54.223: D/dalvikvm(764): GC_CONCURRENT freed 262K, 3% free 11686K/12039K, paused 115ms+110ms, total 396ms

推荐答案

您必须计算所有标记.为此

You have to calculate of all the markers. To do so

LatLngBounds.Builder builder = new LatLngBounds.Builder();
for each (Marker m : markers) {
    builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();

现在获取CameraUpdateFactory:

int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);

最后将摄像机移动到这样的一组标记上:

Finally move camera on group of markers like this :

googleMap.moveCamera(cu);

或者如果您想要动画:

googleMap.animateCamera(cu);

这篇关于如何在Android的Google Map中聚焦于Marker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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