简体   繁体   中英

android googlemaps Marker Doesn't appear

I'm trying do a map in my app and that there will be a default marker here is the google maps activity xml I have tried countless time to make the marker appear but i just doesn't see it, i checked few threads here but nothing helped.

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.nir.nestleapp.GoogleMapsActivity"
    map:cameraBearing="0"
    map:cameraTargetLat="31.926597"
    map:cameraTargetLng="34.800077"
    map:cameraTilt="30"
    map:cameraZoom="17"
    />

this is the java googlemaps activity which im trying to put the marker on:

package com.example.nir.nestleapp;

import android.content.pm.PackageManager;
import android.graphics.Camera;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.IOException;
import java.util.Map;

public class GoogleMapsActivity extends FragmentActivity implements OnMapReadyCallback{
    private GoogleMap mMap;
    private LatLng nessziona=new LatLng(31.926597,34.800077);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
            mMap.addMarker(new MarkerOptions()
                    .position(nessziona)
                    .title("Hello world"));
        mMap.moveCamera((CameraUpdateFactory.newLatLng(nessziona)));
    }



}
     MarkerOptions options = new MarkerOptions();

            // Setting the position of the marker
            // Set your multiple driver latitude and langitude below
            options.position(CURRENT_LOCATION);

//Markers from drawable
options.icon(BitmapDescriptorFactory.fromResource(R.drawable.ambulance));

//For custom marker
options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))

            options.title("Driver 1");
            options.snippet("Driver-1");
            mMap.addMarker(options);

Hope this helps you. If you have any issue please feel free to ask. Happy to help you :) :)

Try to use an icon for your marker!

Map.addMarker(new MarkerOptions()
                    .position(nessziona)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
                    .title("Hello world"));

Explanation:

This line

.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))

will add an icon for your marker, a default red color icon.

refer to this link , for more info

Your code seems correct, so first check logs(may be map-key create some problem) and add this one in your code also..

@Override 
public void onMapReady(GoogleMap googleMap) {
LatLng nessziona=new LatLng(31.926597,34.800077);
    mMap = googleMap;
 mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mMap.addMarker(new MarkerOptions()
                .position(nessziona)
                .title("Hello world"));
    mMap.moveCamera((CameraUpdateFactory.newLatLng(nessziona,17f)));
} 

xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nir.nestleapp.GoogleMapsActivity"

/>

In my project I have this and it work :

@Override
public void onMapReady(GoogleMap googleMap) {

    mMap = googleMap;

    BitmapDescriptor markerCustomIcon = BitmapDescriptorFactory.fromResource(R.drawable.trash_recyclebin_empty_closed);
    BitmapDescriptor markerDefaultIcon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET);
    List<Marker> markersList = new ArrayList<Marker>();
    for (int i = 0; i < pgoList.size(); i++) {
        LatLng pos = new LatLng(pgoList.get(i).location.lat, pgoList.get(i).location.lon);
        MarkerOptions marker = new MarkerOptions()
                .position(pos)
                .title(pgoList.get(i).address.full)
                .icon(markerCustomIcon != null ? markerCustomIcon : markerDefaultIcon);
        mMap.addMarker(marker);
        markersList.add( mMap.addMarker(marker));
    }
    builder = new LatLngBounds.Builder();
    for (Marker m : markersList) {
        builder.include(m.getPosition());
    }
    int padding = 0;
    LatLngBounds bounds = builder.build();
    cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
    mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
        @Override
        public void onMapLoaded() {
            mMap.animateCamera(cu);
        }
    });

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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