我想在谷歌地图上添加标记来制作多边形

时间:2017-05-13 11:23:54

标签: android google-maps

这是代码

我已尝试多次添加标记到地图请指导。

public class MainActivity extends Activity
    implements OnMapClickListener, OnMapLongClickListener, OnMarkerClickListener, OnMapReadyCallback {

final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;

Location myLocation;
TextView tvLocInfo;

boolean markerClicked;
PolygonOptions polygonOptions;
Polygon polygon;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvLocInfo = (TextView) findViewById(R.id.locinfo);

    FragmentManager myFragmentManager = getFragmentManager();
    MapFragment myMapFragment
            = (MapFragment) myFragmentManager.findFragmentById(R.id.map);
    myMapFragment.getMapAsync(this);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        myMap.setMyLocationEnabled(true);

        myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);



        return;


    }

    myMap.setOnMapClickListener(this);
    myMap.setOnMapLongClickListener(this);
    myMap.setOnMarkerClickListener(this);


    markerClicked = false;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_legalnotices:
            String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
                    getApplicationContext());
            AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
            LicenseDialog.setTitle("Legal Notices");
            LicenseDialog.setMessage(LicenseInfo);
            LicenseDialog.show();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {

    super.onResume();

    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

    if (resultCode == ConnectionResult.SUCCESS) {
        Toast.makeText(getApplicationContext(),
                "isGooglePlayServicesAvailable SUCCESS",
                Toast.LENGTH_LONG).show();
    } else {
        GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
    }

}

@Override
public void onMapClick(LatLng point) {
    tvLocInfo.setText(point.toString());
    myMap.animateCamera(CameraUpdateFactory.newLatLng(point));

    markerClicked = false;
}

@Override
public void onMapLongClick(LatLng point) {
    tvLocInfo.setText("New marker added@" + point.toString());
    myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));

    markerClicked = false;
}

@Override
public boolean onMarkerClick(Marker marker) {

    if (markerClicked) {

        if (polygon != null) {
            polygon.remove();
            polygon = null;
        }

        polygonOptions.add(marker.getPosition());
        polygonOptions.strokeColor(Color.RED);
        polygonOptions.fillColor(Color.BLUE);
        polygon = myMap.addPolygon(polygonOptions);
    } else {
        if (polygon != null) {
            polygon.remove();
            polygon = null;
        }

        polygonOptions = new PolygonOptions().add(marker.getPosition());
        markerClicked = true;
    }

    return true;
}

@Override
public void onMapReady(GoogleMap googleMap) {

    myMap = googleMap;

    // Add a marker in Sydney and move the camera
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.

        myMap.setMyLocationEnabled(true);
        return;
    }



}

}

我从GoogleMapV2获取此代码,但我无法在谷歌地图上制作标记

如何将标记添加到谷歌地图

请帮助

1 个答案:

答案 0 :(得分:0)

我将map称为id

var myMarker = new google.maps.Marker({
             position: latlng,
             map: map,
             title:"fggfh.Pvt.Ltd"
             });