2016-11-18 99 views
0

我试图在谷歌地图中创建一个动态标记,但地图没有加载,地图活动确实加载,但它没有创建标记。 这里是代码:添加动态标记到地图

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使用束以接收所述信息从先前的activty,其中发送两个双打和一个串发送。出于某种原因,当我在手机上运行它时会打开maps_activity,但它没有创建标记。 我不会粘贴整个XML代码,但地图对象被命名为“地图”

+0

也许你已经交换了经度和纬度......你尝试过'LatLng lugar = new LatLng(posy,posx);'? – antonio

+0

尝试命名你的额外的经度和纬度,所以你不要混淆命令 –

+0

'LatLng'实现'Parcelable',所以它可以通过'putParcelable'直接添加到'Bundle'并使用'getParcelable'获取 – antonio

回答

0

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