添加标记不工作android

时间:2014-10-18 22:00:12

标签: android maps

public class MapActivity extends SherlockFragmentActivity{
// Google Map
private GoogleMap googleMap;
String longitude="0";
String latitude="0";
String title="";
@Override
protected void onCreate(Bundle savedInstanceState) {
    try
    {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_map); 
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        try {
            // Loading map
            initilizeMap();

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

        }


}

/**
 * function to load map. If map is not created it will create it for you
 * */
private void initilizeMap() {
    try
    {
          if (googleMap == null) {
        String longitude="27.175015";
        String latitude="78.042155";
        double savedLat = Double.parseDouble(longitude);
        double savedLng =Double.parseDouble(latitude);
        LatLng cameraLatLng = new LatLng(savedLat, savedLng);
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                R.id.map)).getMap();
        googleMap.addMarker(new MarkerOptions()
        .position(new LatLng( Float.parseFloat(latitude), Float.parseFloat(longitude))).title("Marker")
        .title("mazen"));
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(cameraLatLng, 17));
        // check if map is created successfully or not
        if (googleMap == null) {

        }
          }
    }
    catch(Exception e)
    {

    }
}

@Override
protected void onResume() {
    super.onResume();
    initilizeMap();
}
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        finish();
        overridePendingTransition(R.anim.dux_sld_lft_in,
                R.anim.dux_sld_lft_out);
        return true;
    }else {
        return super.onOptionsItemSelected(item);
    }
}
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
     overridePendingTransition(R.anim.dux_sld_lft_in, R.anim.dux_sld_lft_out);
}
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    return true;
}

}

我在这里添加标记,但没有标记显示原因?我怎样才能在多个地方添加多个标记?这非常有趣的错误...因为我真的做了我读的教程 希望有人能为我提供完成这项任务所需的必要帮助

1 个答案:

答案 0 :(得分:0)

使用addMarker()

喜欢:

myMap.addMarker(new MarkerOptions().position(yourLatLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.yourmarkericon)));

其中 myMap GoogleMap的对象,而 yourLatlng Latlng您要添加标记和 yourmarkericon 是您要在地图上显示的图标

参考文献

https://developers.google.com/maps/documentation/android/marker

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Marker

简单教程

http://bon-app-etit.blogspot.be/2012/12/add-informationobject-to-marker-in.html