2017-11-29 13 views
0

以下は私のMapsActivity.java、 私はgetCurrentLocationを取得してそこにマーカーを配置しようとしています。 アプリを実行すると、地図上にマーカーが表示されません。何が悪かったのか? 助けてくれてありがとう。上記地図上にマーカーが表示されません

import android.Manifest; 
import android.content.pm.PackageManager; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.app.FragmentActivity; 
import android.util.Log; 

import com.bustracker.usc.myapplication.R; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

import java.io.IOException; 
import java.util.List; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 
    private GoogleMap mMap; 
    private LocationManager manager; 
    private LocationListener locationListener; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     Log.d("INSIDE ONCREATE", "TRUE"); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     // 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); 
     //get the location service 
     manager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     //request the location update thru location manager 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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. 

      return; 
     } 
     Log.d("BFORE ISPROVIDERENABLED", "TRUE"); 
     if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
      manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() { 

       @Override 
       public void onLocationChanged(Location location) { 
        //get the latitude and longitude from the location 
        double latitude = location.getLatitude(); 
        double longitude = location.getLongitude(); 
        Log.d("LATLNG", latitude+" " +longitude); 
        //get the location name from latitude and longitude 
        Geocoder geocoder = new Geocoder(getApplicationContext()); 
        try { 
         List<Address> addresses = 
           geocoder.getFromLocation(latitude, longitude, 1); 
         String result = addresses.get(0).getSubLocality() + ":"; 
         result += addresses.get(0).getLocality() + ":"; 
         result += addresses.get(0).getCountryCode(); 
         LatLng latLng = new LatLng(latitude, longitude); 
         mMap.addMarker(new MarkerOptions().position(latLng).title(result)); 
         mMap.setMaxZoomPreference(20); 
         mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 

       @Override 
       public void onStatusChanged(String provider, int status, Bundle extras) { 

       } 

       @Override 
       public void onProviderEnabled(String provider) { 

       } 

       @Override 
       public void onProviderDisabled(String provider) { 

       } 
      }); 
     }else if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
      manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() { 
       @Override 
       public void onLocationChanged(Location location) { 
        //get the latitude and longitude from the location 
        double latitude = location.getLatitude(); 
        double longitude = location.getLongitude(); 
        //get the location name from latitude and longitude 
        Geocoder geocoder = new Geocoder(getApplicationContext()); 
        try { 
         List<Address> addresses = 
           geocoder.getFromLocation(latitude, longitude, 1); 
         String result = addresses.get(0).getSubLocality() + ":"; 
         result += addresses.get(0).getLocality() + ":"; 
         result += addresses.get(0).getCountryCode(); 
         LatLng latLng = new LatLng(latitude, longitude); 
         mMap.addMarker(new MarkerOptions().position(latLng).title(result)); 
         mMap.setMaxZoomPreference(20); 
         mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 

       @Override 
       public void onStatusChanged(String provider, int status, Bundle extras) { 

       } 

       @Override 
       public void onProviderEnabled(String provider) { 

       } 

       @Override 
       public void onProviderDisabled(String provider) { 

       } 
      }); 
     } 

    } 


    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @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)); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     manager.removeUpdates(locationListener); 
     Log.i("onPause...","paused"); 
    } 
} 

私はgetCurrentLocationにしようと、そこにマーカーを配置しています私のMapsActivity.java、 です。 アプリを実行すると、地図上にマーカーが表示されません。何が悪かったのか? 助けてくれてありがとう。適切にそれが動作しない場合は、あなたの場所を取得している場合を見てみてください、onMapReadyメソッド内のマップについてのマーカーとすべてのものを作成するための

+1

OnMapReady()メソッドでlat-longを設定します。 –

答えて

0

てみ

は、Pd、それはあなたのお役に立てば幸いです。私の英語のため申し訳ありませんあなたが好きそれを追加し、マーカーを設定するには、カスタムアイコンを追加したい場合は

0

では、有効なlatLng

OR

を取得していることを確認してください:

map.addMarker(new MarkerOptions().position(latLng).title(result)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 
関連する問題