android googlemaps Marker不会出现

时间:2017-02-21 11:50:12

标签: java android google-maps android-studio

我正在尝试在我的应用中制作地图,并且会有一个默认标记 这是谷歌地图活动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"
    map:cameraBearing="0"
    map:cameraTargetLat="31.926597"
    map:cameraTargetLng="34.800077"
    map:cameraTilt="30"
    map:cameraZoom="17"
    />

这是java googlemaps活动,我试图将标记放在:

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)));
    }



}

4 个答案:

答案 0 :(得分:0)

     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);

希望这会对你有所帮助。如果您有任何问题,请随时提出。很高兴为您服务:):)

答案 1 :(得分:0)

尝试使用标记的图标!

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

<强>解释

这一行

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

将为您的标记添加一个图标,默认的红色图标。

请参阅此link,了解详情

答案 2 :(得分:0)

您的代码似乎是正确的,所以首先检查日志(可能是map-key创建了一些问题)并在代码中添加了这个...

@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"

/>

答案 3 :(得分:0)

在我的项目中,我有这个并且它有效:

@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);
        }
    });

}