向地图

时间:2016-11-18 13:34:22

标签: java android google-maps

我试图在谷歌地图中创建一个动态标记,但地图不会加载,地图活动会加载,但它不会创建de marker。 这是代码:

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

我使用捆绑包来接收从前一个活动发送的信息,该活动发送两个双打和一个字符串。出于某种原因,当我在手机上运行时会打开maps_activity,但它不会创建标记。 我不会粘贴整个xml代码,但地图对象名为“map”

1 个答案:

答案 0 :(得分:0)

事实证明代码很好,问题是我正在开发的Android版本,因为它是我手机上的旧版本,因为某些原因它只是不会打开谷歌地图正确的,因此我不得不将孔应用程序重新制作到我手机上的相同Android版本。