无法在Google地图中添加多个标记

时间:2015-09-25 19:07:05

标签: android google-maps google-maps-markers google-maps-android-api-2 google-maps-api-2

我想在我的Android应用中更简单地实施Google Map。我可以启动谷歌地图,但我无法在那里显示多个标记。如果我保持纬度和经度值以及硬编码的地方名称然后事情有效,但动态地使用变量和字符串值我无法显示它们。 我也经历了许多人在这里建议的link。但我真正想要的是更简单的实现。

以下是我在主要活动中使用的代码:

public class Map_Activity_With_Fragment extends AppCompatActivity implements OnMapReadyCallback {

private GoogleMap googleMap;
String Origin, Destination, Start_Date, Num_Adult, Num_Infant, Origin_Map, Destination_Map;
Context context;
List<Address> Addr_Origin, Addr_Dest;
double latitude_origin, longitude_origin, latitude_destination, longitude_destination;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    Origin = extras.getString(MainActivity.ORIGIN);
    Destination = extras.getString(MainActivity.DESTINATION);
    Start_Date = extras.getString(MainActivity.START_DATE);
    Num_Adult = extras.getString(MainActivity.ADULT);
    Num_Infant = extras.getString(MainActivity.INFANT);
    Origin_Map = extras.getString(MainActivity.ORIGIN_MAP);
    Destination_Map = extras.getString(MainActivity.DESTINATION_MAP);
    context = Map_Activity_With_Fragment.this;
    setTitle("Location Map");
    setContentView(R.layout.activity_map__with__fragment);

    try
    {
        initializeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onMapReady(GoogleMap googleMap)
{
    AddMarkers(googleMap);
}


@Override
protected void onResume() {
    super.onResume();
    initializeMap();
}

private void initializeMap()
{
    if (googleMap == null) {
        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
}

private void AddMarkers(GoogleMap googleMap)
{
    try {
        Geocoder geocoder = new Geocoder(context);
        Addr_Origin = geocoder.getFromLocationName(Origin_Map, 1);
        Addr_Dest = geocoder.getFromLocationName(Destination_Map, 1);
        if (Addr_Origin.size() > 0) {
            latitude_origin = Addr_Origin.get(0).getLatitude();
            longitude_origin = Addr_Origin.get(0).getLongitude();
        }
        if (Addr_Dest.size() > 0) {
            latitude_destination = Addr_Dest.get(0).getLatitude();
            longitude_destination = Addr_Dest.get(0).getLongitude();
        }
        Marker m1 = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude_origin, longitude_origin)).title(Origin_Map));
        Marker m2 = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude_destination, longitude_destination)).title(Destination_Map));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

请帮帮我! 提前谢谢!

1 个答案:

答案 0 :(得分:1)

好的,所以我要更新自己的问题。

我正在API级别16的设备上测试这些代码更改,因此标记和折线无法正确呈现。然后我在API级别19的设备上测试了相同的代码,它工作得很好。我没有为此修改代码的任何部分。事情正在发挥作用。 :)