简体   繁体   English

通过onmapclick在Google地图上添加标记

[英]Add Marker on google map via onmapclick

First thing to say: i'm a novice programmer... i'm trying to modifying an open source i've found on github and i have this problem: adding markers don't work when i tap while debugging my app. 首先要说的是:我是一个新手程序员...我正在尝试修改我在github上发现的一个开放源代码,但我遇到了这个问题:在调试应用程序时点击添加标记不起作用。

@Override
public void onMapClick(LatLng latLng) {
    if (mMarker == null) {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.title(latLng.toString())
                .position(latLng)
                .draggable(true);
        mMarker = mMap.addMarker(markerOptions);
    } else {
        mMarker.setPosition(latLng);
        mMarker.setTitle(latLng.toString());
        mMarker.showInfoWindow();
    }
}

The public class: 公开课:

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks, OnMapReadyCallback, GoogleMap.OnMapClickListener {

Beside my if-else code, i tried many of the codes examples i've read here on stackoverflow and on google api docs, but still do nothing when i tap the phone screen. 除了我的if-else代码,我尝试了在stackoverflow和google api docs上阅读过的许多代码示例,但是当我点击手机屏幕时仍然什么也没做。

Sorry for my english... 对不起我的英语不好...

As @Pavneet_Singh wrote, this did the trick: 就像@Pavneet_Singh所写的那样,它起到了作用:

Before: 之前:

 @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
}

After: 后:

 @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setOnMapClickListener(this);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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