*********问题解决了*********
我想在谷歌地图上找到我自己的位置,当我按下我的应用程序中的按钮,所以我写了这个代码,但它没有在地图上找到我只是一张地图上没有任何标记我已经看到其他人问这个,但我想我错过了一些东西!我真的需要帮助
我的主要类
public class MainActivity extends AppCompatActivity {
private Button mbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mbutton = (Button) findViewById(R.id.map);
mbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,MapsActivity.class);
startActivity(intent);
}
});
}
}
和我的地图类
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
private void setUpMapIfNeeded() {
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location arg0) {
// TODO Auto-generated method stub
mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!"));
}
});
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
try{
mMap.setMyLocationEnabled(true);
setUpMapIfNeeded();
} catch(SecurityException e){
Toast.makeText(getApplicationContext(),"App doesn't have the permission to gps... ",Toast.LENGTH_SHORT).show();
}}}
这是我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.secu" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps"></activity>
</application>
</manifest>
答案 0 :(得分:0)
将setUpMapIfNeeded()
方法替换为下面的方法:
private void setUpMapIfNeeded()
{
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
if (mMap != null) // not sure this is usefull at this point
{
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener()
{
@Override
public void onMyLocationChange(Location arg0)
{
mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!"));
}
});
}
}
您可以在map == null
检查中添加您的监听器,这将无法正常工作。
答案 1 :(得分:0)
试试这个:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<body ng-app="app">
<div ng-controller="TestCtrl">
<input style="width:70%" type=text ng-model=search placeholder="Type key names which are either 'Auction Platform' or 'Finansme' ">
<div ng-repeat="item in keys | filter:search">
{{items[item][0].version_id}} | {{items[item][0].version_name}}
</div>
</div>
</body>
将这段代码放在if (ContextCompat
.checkSelfPermission(
getContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED
&&
ContextCompat.checkSelfPermission(
getContext(),
android.Manifest.permission.ACCESS_COARSE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
map.setMyLocationEnabled(true);
map.getUiSettings().setMyLocationButtonEnabled(true);
}
if条件检查位置权限是否可用。 我发布的代码中的重要部分是:
setUpMapIfNeeded()
JSharma的答案使用融合位置提供程序,这是您使用的API,如果您想要更多地控制您的位置更新,它可以让您详细控制您希望如何接收位置更新,频率等等
如果你想要一个简单的位置更新,试试我的答案,看看是否有用,如果你需要更多的控制,你就是JSharma的
答案 2 :(得分:0)
试试这个
public class MainActivity extends AppCompatActivity implements
OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
SupportMapFragment mMap;
LatLng CURRENT_LOCATION;
GoogleApiClient mGoogleClient;
GoogleMap mGoogleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int result=GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(result!=1)
{
Toast.makeText(this, "Availble", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Not Availble", Toast.LENGTH_SHORT).show();
}
mGoogleClient= new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mMap= (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.my_map);
mMap.getMapAsync(this);
}
@Override
protected void onStart() {
super.onStart();
mGoogleClient.connect();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap=googleMap;
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
}
@Override
public void onConnected(Bundle bundle) {
Location mCurrentLocation=LocationServices.FusedLocationApi.getLastLocation(mGoogleClient);
if(mCurrentLocation!=null)
{
//here is your current location with lat & lon
double lat=mCurrentLocation.getLatitude();
double lon=mCurrentLocation.getLongitude();
Log.e(">>>",String.valueOf(lat));
Log.e(">>>",String.valueOf(lon));
MarkerOptions marker=new MarkerOptions();
marker.position(mCurrentLocation);
marker.title("Android");
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
mGoogleMap.addMarker(marker);
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mohali,8));
}
else {
Toast.makeText(this, "Empty Location", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sts.mapdemoandroid">
<uses-permission android:name="android.permission.INTERNET">
</usespermission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION">
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
</uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBk4QJpLT8JoYfTpPMkxgDDFXJQGcmnHCg">
</meta-data>
</application>