[英]How can I add a Marker in google map?
I want to add one Marker in Google map. 我想在Google地图中添加一个标记。 I successfully loaded map in my app but when I tried to add a Marker, the app got crashed.
我已在应用程序中成功加载地图,但是当我尝试添加标记时,应用程序崩溃了。 I don't know why.
我不知道为什么 Please some one help me!
请有人帮我!
My code: 我的代码:
public class BasicMapActivity extends FragmentActivity {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo);
setUpMap();
}
@Override
protected void onResume() {
super.onResume();
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
Layout.xml file: Layout.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
Create a method setUpMapIfNeeded()
and called on onResume()
and onCreate()
创建一个方法
setUpMapIfNeeded()
并在onResume()
和onCreate()
上调用
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
First you need to obtain the map from the SupportMapFragment
and then you add Marker
into map using 首先,你需要获得从地图
SupportMapFragment
然后添加Marker
使用到的地图
mMap.addMarker(new MarkerOptions().position(new LatLng(Your_lat, Your_long)).title("Marker"));
Try This 尝试这个
private void setUpMap() {
Marker pos_Marker = googleMap.addMarker(new MarkerOptions().position(starting).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_laumcher)).title("Starting Location").draggable(false));
pos_Marker.showInfoWindow();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.