简体   繁体   中英

add dynamic marker to maps

Im trying to create a dynamic marker in google maps but the map doesen't load, the maps activity does load but it doesen't create de marker. here is the code:

    import android.support.v4.content.ContextCompat;
    import android.support.v4.app.FragmentActivity;
    import android.os.Bundle;
    import android.widget.Toast;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.CameraPosition;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;
    import android.content.pm.PackageManager;





public class Mapa extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
public double posx;
public double posy;
public String desc;

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

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    //SupportMapFragment googleMap=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

    Bundle b = getIntent().getExtras();
    posx= b.getDouble("PosicionX");
    posy= b.getDouble("PosicionY");
    desc = b.getString("Descripcion");


}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {

    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng lugar = new LatLng(posx, posy);
    mMap.addMarker(new MarkerOptions().position(lugar).title(desc));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(lugar));
}
}

Im using a bundle to receive the information send from the previous activty, which sends two doubles and one string. for some reason it opens the maps_activity when i run it on my phone but it doesen't create the marker. I wont paste the whole xml code, but the map object is named "map"

事实证明,代码很好,问题出在我正在开发的Android版本上,因为它是我手机上的Android版本的较旧版本,由于某种原因,它无法正确打开google maps,因此我不得不将Hole应用程序重新制作为手机上的Android版本。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM