2017-02-22 67 views
0

如何在Google地图API v3中针对android长按添加标记?在Stack Overflow本身有答案,但它们是针对Google Maps API v2的。在Google地图API v3中长时间添加标记

public class advertiserMap extends FragmentActivity implements OnMapReadyCallback { 

private GoogleMap mMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_advertiser_map); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
} 

//for searching for a location 
public void onMapSearch(View view) { 
    EditText locationSearch = (EditText) findViewById(R.id.editText); 
    String location = locationSearch.getText().toString(); 
    List<Address> addressList = null; 

    if (location != null || !location.equals("")) { 
     Geocoder geocoder = new Geocoder(this); 
     try { 
      addressList = geocoder.getFromLocationName(location, 1); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     Address address = addressList.get(0); 
     LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude()); 
     mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); 
    } 
} 

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

    // Add a marker in Sydney and move the camera 
    LatLng sydney = new LatLng(-34, 151); 
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
} 

}

+0

你有任何示例代码? –

+0

没有v3这样的东西,android是v2。 https://developers.google.com/maps/documentation/android-api/start 除非你的意思是你想在你的应用中使用Javascript版本(v3) – tyczj

+0

@Drew我刚刚编辑帖子 – jo12345678

回答

3

由于@tyczj指出,有一个为Android的谷歌地图API V3没有,所以你可能正在使用V2。

这就是说,要完成你想要的,请在你的mMap对象中调用setOnMapLongClickListener,然后在MapLongClick中根据需要添加标记。你应该在onMapReady方法做到这一点:

@Override 
public void onMapReady(GoogleMap googleMap) { 

    ... 

    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() { 
     @Override 
     public void onMapLongClick(LatLng latLng) { 
      googleMap.addMarker(new MarkerOptions() 
        .position(latLng) 
        .title("Your marker title") 
        .snippet("Your marker snippet")); 
     } 
    }); 
} 

//编辑:

如果你想保持目前在同一时间只有一个标志,你应该宣布在全球经济活动的范围的标记,然后在onMapLongClick,如果标志已经存在,而不是创建一个新的标记,只需更新它的位置:

public class advertiserMap extends FragmentActivity implements OnMapReadyCallback { 

    // Declare marker globally 
    Marker myMarker; 

    ... 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 

     ... 

     mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() { 
      @Override 
      public void onMapLongClick(LatLng latLng) { 

       // First check if myMarker is null 
       if (myMarker == null) { 

        // Marker was not set yet. Add marker: 
        myMarker = googleMap.addMarker(new MarkerOptions() 
         .position(latLng) 
         .title("Your marker title") 
         .snippet("Your marker snippet")); 

       } else { 

        // Marker already exists, just update it's position 
        myMarker.setPosition(latLng); 

       } 
      } 
     }); 
    } 
} 

如果这样做,记得要经常检查,如果你的标志是不是在你的代码操作之前NULL。

+0

感谢队友,它像一个魅力,但我怎样才能使地图一次只有一个标记?例如,如果我已经长时间按在某个地方,并认定它不是正确的位置,并且我长时间按其他位置,则以前的标记需要消失。这需要是这种情况,因为我将不得不检索标记的latlng。 – jo12345678

+0

我编辑了答案,补充了这种情况。看看@JolanGoburdhun –

+0

非常感谢! – jo12345678

相关问题