2015-06-10 22 views
0

我想在地圖上添加標記,只需長按一下即可。但它不起作用。正常點擊正在工作。 這是我的代碼。從我的列表中插入具有不同圖標的標記可繪製

公共類地圖擴展FragmentActivity實現LocationListener的{

GoogleMap googleMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //show error dialog if GoolglePlayServices not available 
    if (!isGooglePlayServicesAvailable()) { 
     finish(); 
    } 
    setContentView(R.layout.map); 
    SupportMapFragment supportMapFragment = 
      (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap); 
    // Getting GoogleMap object from the fragment 
    googleMap = supportMapFragment.getMap(); 
// Enabling MyLocation Layer of Google Map 
    googleMap.setMyLocationEnabled(true); 

// Getting LocationManager object from System Service LOCATION_SERVICE 
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
// Creating a criteria object to retrieve provider 
    Criteria criteria = new Criteria(); 
// Getting the name of the best provider 
    String bestProvider = locationManager.getBestProvider(criteria, true); 
    Location location = locationManager.getLastKnownLocation(bestProvider); 
    if (location != null) { 
     onLocationChanged(location); 
    } 
    locationManager.requestLocationUpdates(bestProvider, 20000, 0, this); 


} 



@Override 
public void onLocationChanged(Location location) { 
    TextView locationTv = (TextView) findViewById(R.id.latlongLocation); 
// Getting latitude of the current location 
    double latitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 

    LatLng latLng = new LatLng(latitude, longitude); 
    googleMap.addMarker(new MarkerOptions().position(latLng).title("you are here")); 
    // Showing the current location in Google Map 
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    // Zoom in the Google Map 
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(5)); 
// Setting latitude and longitude in the TextView tv_location 
    locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude); 
} 

public void onMapClick (LatLng point) { 
    // Do Something 
    googleMap = 
    ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap)).getMap(); 
    googleMap.addMarker(new MarkerOptions() 
    .position(point) 
    .title("TouchPoint")); 
} 
public void onMapLongClick(LatLng point) { 

    googleMap.addMarker(new MarkerOptions().position(point).title(
      point.toString())); 

    Toast.makeText(getApplicationContext(), 
      "New marker [email protected]" + point.toString(), Toast.LENGTH_LONG) 
      .show(); 
} 

@Override 
public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 
} 

private boolean isGooglePlayServicesAvailable() { 
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
    if (ConnectionResult.SUCCESS == status) { 
     return true; 
    } else { 
     GooglePlayServicesUtil.getErrorDialog(status, this, 0).show(); 
     return false; 
    } 
} 





@Override 
public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 

} 

} 

我怎樣才能解決這個問題?

回答

0

在的onCreate

googleMap.setOnMapLongClickListener(longClickListener); 

活性

private OnMapLongClickListener longClickListener = new OnMapLongClickListener() { 

    @Override 
    public void onMapLongClick(LatLng point) { 
     //do work } 
} 
+0

裏面當添加googleMap.setOnMapLongClickListener(longClickListener);在onCreate我有一個erreur似乎longClickListener不能解析爲一個變量 –

相關問題