添加多个标记Google Maps [英] Adding Multiple Markers Google Maps

查看:65
本文介绍了添加多个标记Google Maps的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试全部添加标记,具体取决于用户正在进行的活动.例如,如果用户在location1活动中并单击按钮以打开地图,则用户应在location1所在的位置打开带有标记的Google地图.或者,如果用户在location2活动中并单击按钮以打开地图,则用户应在位置2所在的位置打开带有标记的Google Maps.

I am trying to add a marker all depending on what activity the user is on. For example, if the user is in the location1 activity and clicks the button to open maps, it should open Google Maps with a marker at where location1 is. Alternatively, if the user is in the location2 activity and clicks the button to open maps, it should open Google Maps with a marker at where location 2 is.

当他们单击一项活动时,它就可以正常工作,它将它们带到Google地图,并在该位置处有一个标记.我只是试图复制代码并将其粘贴到其中,并在其中粘贴编辑过的名称,但是如果我单击其他活动来转到地图,则会将我带到与以前相同的标记.

I have it working when they click on one activity, it brings them to Google Maps and has a marker at where that location is. I have simply tried to copy the code and paste it below with the edited names in it, but if I click a different activity to go to maps, it brings me to the same marker as previously.

我的GoogleMapsActivity代码如下:

My Code for GoogleMapsActivity is below:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

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

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    //mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    // Add a marker at the Oval and move the camera
    LatLng oval = new LatLng(53.3484013, -6.2605243);
    mMap.addMarker(new MarkerOptions().position(oval).title("Oval Pub"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(oval));

    /*
    // Add a marker at Diceys and move the camera
    LatLng diceys = new LatLng(53.3358088,-6.2636688);
    mMap.addMarker(new MarkerOptions().position(diceys).title("Diceys Nightclub"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(diceys));
    */
}

public void changeType(View view)
{
    if(mMap.getMapType() == GoogleMap.MAP_TYPE_NORMAL)
    {
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    }
    else
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}}

如您所见,我已注释掉尝试在其他位置添加标记的代码,但这似乎将我带到了与上述相同的位置.

As you can see, I have commented out the code where I have tried adding a marker for a different location, but it seems to bring me to the same location as above.

不确定这是否简单,因为我是Android中的Google Maps的新手.

Not sure if this is something simple or not as I am new to Google Maps in Android.

任何帮助将不胜感激.

谢谢.

推荐答案

ArrayList<MarkerData> markerArray = new ArrayList<MarkerData>();
for(int i = 0 ; i < markersArray.size() ; i++ ) {

createMarker(markersArray.get(i).getLatitude(), markersArray.get(i).getLongitude(), markersArray.get(i).getTitle(), markersArray.get(i).getSnippet(), markersArray.get(i).getIconResID());
}
....
protected void createMarker(double lat, double lon, String title, String snippet, int iconResID) {
return googleMap.addMarker(new MarkerOptions()
        .position(new LatLng(lat, lon))
        .anchor(0.5f, 0.5f)
        .title(title)
        .snippet(snippet);
        .icon(BitmapDescriptorFactory.fromResource(iconResID)));
}

这篇关于添加多个标记Google Maps的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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