添加多个标记Google地图

时间:2016-04-18 12:54:07

标签: android google-maps android-studio google-maps-api-3

我正在尝试添加一个标记,具体取决于用户所处的活动。例如,如果用户处于location1活动并单击按钮以打开地图,则应使用location1所在的标记打开Goog​​le地图。或者,如果用户处于location2活动并单击按钮以打开地图,则应使用位置2处的标记打开Goog​​le地图。

我点击一项活动时就可以使用它,它会将它们带到Google地图,并在该位置有一个标记。我只是试图复制代码并将其粘贴在下面,并在其中添加已编辑的名称,但如果我点击其他活动转到地图,则会将我带到与之前相同的标记。

我的GoogleMapsActivity代码如下:

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);
}}

正如您所看到的,我已经注释掉了我尝试为其他位置添加标记的代码,但它似乎将我带到了与上面相同的位置。

不确定这是否简单,因为我不熟悉Android中的Google地图。

非常感谢任何帮助。

谢谢。

3 个答案:

答案 0 :(得分:2)

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)));
}

答案 1 :(得分:0)

将它们捆绑在一起:

function samplegui_OpeningFcn(hObject, ~, handles, varargin)

handles.timer = timer(...
       'ExecutionMode', 'fixedRate', ...   % Run timer repeatedly
       'Period', 300, ...                % Initial period.
       'TimerFcn', {@open,hObject}); % Specify callback

handles.output = hObject;
handles.vec_A=[];
handles.vec_B=[];
handles.vec_C=[];
guidata(hObject, handles);

function open_Callback(hObject, eventdata, handles) % push button to receive serial data.

cnt=0;

while 1

       % Getting data  from Serial Port
        get_lines=fgets(handles.se) % getting data from serial port 
           if~isempty(get_lines)
            cnt=cnt+1;   
       if strfind(get_lines,'T')   %Parsing data
       handles.vec_A=[handles.vec_A;[timet newword]];
       plot(handles.vec_A(:,1),handles.vec_A(:,2:end),'r'); % plotting

       % Same follows for parsing and plot vec_B and Vec_C
       drawnow(); % to update the Plots
       end
     end
Pause(.05);

      start(handles.timer);  % saving the data
      dlmwrite('My_sample1.txt',handles.vec_A);
      dlmwrite('My_sample2.txt',handles.vec_B);
      dlmwrite('My_sample3.txt',handles.vec_C);
      stop(handles.timer);
end
guidata(hObject, handles);

答案 2 :(得分:0)

在First Location活动中:

Intent i = new Intent(FirstLocationActivity.this, MapsActivity.class);   
  String keyLatitude  = ""; //enter the value
  String keyLongitude  = ""; //enter the value
  i.putExtra("latitude", keyLatitude );
  i.putExtra("longitude", keyLongitude );
 startActivity(i);

同样对活动两个位置做同样的事情,在额外的

中添加相应的纬度和经度

在mapsActivity中,

 String latitude="";
    String longitude ="";

 public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table);

        Bundle bundle = getIntent().getExtras();

       latitude = bundle.getString("latitude");
       longitude = bundle.getString("longitude");

    }

    @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(latitude,longitude);
        mMap.addMarker(new MarkerOptions().position(oval).title("Oval Pub"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(oval));

    }