标记或折线未显示在Google地图中

时间:2018-12-07 08:10:23

标签: java android google-maps manifest

地图活动     包com.example.user.pavai;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

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 com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

public class MapsActivity extends FragmentActivity implements 
OnMapReadyCallback {

    private GoogleMap mMap;
    private  String lat1;
    private String long1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        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);

        Bundle route = getIntent().getExtras();
        lat1 =route.getString("lat1");
        long1 = route.getString("long1");
        // Log.d("lat",lat1);

        //Log.d("long",long1);

        //Toast.makeText(MapsActivity.this,lat1,Toast.LENGTH_SHORT).show();

        //Toast.makeText(MapsActivity.this,long1,Toast.LENGTH_SHORT).show();

    }


    /**
     * 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 ne = new 
LatLng(Double.parseDouble(lat1),Double.parseDouble(long1));
        mMap.addMarker(new MarkerOptions().position(ne).title("Marker in 
Sample"));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new 
LatLng(Double.parseDouble(lat1),Double.parseDouble(long1)), 10));


        //drawing route


       Polyline disp = mMap.addPolyline(new 
PolylineOptions().clickable(true).add(
                new LatLng(Float.parseFloat(lat1),Float.parseFloat(long1)),
                new LatLng(13.118055,80.261054),
                new LatLng(13.131398,80.237838),
                new LatLng(13.146812,80.217019),
                new LatLng(13.205292,80.196488)
        ));

    }
}

清单

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.pavai">

<!--
     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.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />


<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=".Login">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".User" >
        <intent-filter>
        <action android:name="android.intent.action.SEND" />

        <category android:name="android.intent.category.DEFAULT" />

        <data android:mimeType="text/plain" />
    </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">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>


    </activity>

</application>

</manifest>

我正在从先前的活动中获取字符串值并进行解析 将其转换为双精度值并将其用于纬度和经度  但是运行该应用程序时,没有地图和折线  显示了我该如何解决此问题

但是当我在其他应用程序上尝试相同的代码时,它可以工作,但是我不知道为什么它不适用于此

1 个答案:

答案 0 :(得分:0)

我得到了我忘记添加getMapAsync方法的答案